You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by "PJ Fanning (Jira)" <ji...@apache.org> on 2021/10/13 10:55:00 UTC
[jira] [Commented] (XMLBEANS-573) add XmlOptions so that
xmlText(XmlOptions) method can be made to output more accurate XML
[ https://issues.apache.org/jira/browse/XMLBEANS-573?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17428142#comment-17428142 ]
PJ Fanning commented on XMLBEANS-573:
-------------------------------------
One proposal would be having something like on XmlOptions.
{noformat}
void setSaveFragmentElementName(QName tagName){noformat}
> add XmlOptions so that xmlText(XmlOptions) method can be made to output more accurate XML
> -----------------------------------------------------------------------------------------
>
> Key: XMLBEANS-573
> URL: https://issues.apache.org/jira/browse/XMLBEANS-573
> Project: XMLBeans
> Issue Type: Improvement
> Affects Versions: Version 2.6, Version 4.0.0, Version 5.0.1
> Reporter: PJ Fanning
> Priority: Major
>
> 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.
>
> {noformat}
> CTText tmpTextNode = CTText.Factory.newInstance();
> tmpTextNode.setStringValue("A");
> XmlOptions opts = new XmlOptions();
> opts.setSaveOuter();
> opts.setSaveNoXmlDecl();
> System.out.println(tmpTextNode.xmlText(opts));{noformat}
> This prints
> {noformat}
> <xml-fragment>A</xml-fragment>{noformat}
> It would be nice to have an XmlOptions option that allowed this to be printed as
> {noformat}
> <text xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">A</text>{noformat}
>
>
> I hit a similar issue in some code I was writing and had to work around it with this method
>
> {noformat}
> 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;
> }{noformat}
>
>
>
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org