Uploaded image for project: 'XMLBeans'
  1. XMLBeans
  2. XMLBEANS-573

add XmlOptions so that xmlText(XmlOptions) method can be made to output more accurate XML

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • Version 2.6, Version 4.0.0, Version 5.0.1
    • None
    • None
    • None

    Description

      See https://bz.apache.org/bugzilla/show_bug.cgi?id=64837

      CTText is in poi-ooxml-full/poi-ooxml-lite and is generated using XMLBeans.

       

      CTText tmpTextNode = CTText.Factory.newInstance();
       tmpTextNode.setStringValue("A");
       XmlOptions opts = new XmlOptions();
       opts.setSaveOuter();
       opts.setSaveNoXmlDecl();
       System.out.println(tmpTextNode.xmlText(opts));

      This prints

      <xml-fragment>A</xml-fragment>

      It would be nice to have an XmlOptions option that allowed this to be printed as

      <text xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">A</text>

       

       

      I hit a similar issue in some code I was writing and had to work around it with this method

       

      static String stripXmlFragmentElement(String xml) throws IOException, SAXException, ParserConfigurationException, TransformerException {
          String startTag = "xml-fragment";
          int pos = xml.indexOf(startTag);
          if (pos >= 0) {
              Document doc = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
              NodeList list = doc.getDocumentElement().getChildNodes();
              for (int i = 0; i < list.getLength(); i++) {
                  if (list.item(i) instanceof Element) {
                      try (StringWriter sw = new StringWriter()) {
                          Transformer transformer = XMLHelper.newTransformer();
                          transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                          transformer.transform(new DOMSource(list.item(i)), new StreamResult(sw));
                          return sw.toString();
                      }
                  }
              }
              return xml.substring(pos + startTag.length());
          }
          return xml;
      }

       

       

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            pj.fanning PJ Fanning
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: