将 XML 字符串追加到 Java 中的元素中

huangapple go评论67阅读模式
英文:

Append XML String to Element in Java

问题

Document doc;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.newDocument();
Element results = doc.createElement("Results");
doc.appendChild(results);
Element row = doc.createElement("Row");
results.appendChild(row);
String columnName = "XML_00805F49916B";
String valueString = "<IBS><Product></Product></IBS>";  // Modify the valueString directly without escaping
Element node = doc.createElement(columnName);
node.appendChild(doc.createCDATASection(valueString));  // Use CDATASection for preserving XML structure
row.appendChild(node);

DOMSource domSource = new DOMSource(doc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
transformer.transform(domSource, sr);
retMsg = sw.toString();
System.out.println("SQL Return message: " + retMsg);
英文:

I have a requirement in which I need to append a XML String to Element in Java. Below code shows what I am doing now:

Document doc;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.newDocument();
Element results = doc.createElement(&quot;Results&quot;);
doc.appendChild(results);
Element row = doc.createElement(&quot;Row&quot;);
results.appendChild(row);
String columnName = &quot;XML_00805F49916B&quot;;
String valueString = &quot;&lt;IBS&gt;&lt;Product&gt;&lt;/Product&gt;&lt;/IBS&gt;&quot;;
Element node = doc.createElement(columnName);
node.appendChild(doc.createTextNode(valueString));
row.appendChild(node);

DOMSource domSource = new DOMSource(doc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, &quot;yes&quot;);
transformer.setOutputProperty(OutputKeys.METHOD, &quot;xml&quot;);
transformer.setOutputProperty(OutputKeys.ENCODING, &quot;UTF-8&quot;);
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
transformer.transform(domSource, sr);
retMsg = sw.toString();
System.out.println(&quot;SQL Return message: &quot; + retMsg);

The above code returns output:

SQL Return message: &lt;Results&gt;&lt;Row&gt;&lt;XML_00805F49916B&gt;&amp;lt;IBS&amp;gt;&amp;lt;Product&amp;gt;&amp;lt;/Product&amp;gt;&amp;lt;/IBS&amp;gt;&lt;/XML_00805F49916B&gt;&lt;/Row&gt;&lt;/Results&gt;

But, I want the output to be:

SQL Return message: &lt;Results&gt;&lt;Row&gt;&lt;XML_00805F49916B&gt;&lt;IBS&gt;&lt;Product&gt;&lt;/Product&gt;&lt;/IBS&gt;&lt;/XML_00805F49916B&gt;&lt;/Row&gt;&lt;/Results&gt;

Please can I have some help how to get the required output?

答案1

得分: 0

一个文本节点不是XML文档结构的一部分。它只是文本。特殊字符 &lt;&gt;&amp; 在文本节点中只是字符,尽管最终的XML表示需要对它们进行转义。

通过插入文本节点无法实现你的目标。你需要插入元素。

从你的评论中可以看出:

> 不幸的是,我从外部源获取到那个名为 valueString 的XML,它的结构每次都可能不同。我需要一种将XML字符串值添加到元素中的通用方法。

你可以将任意XML内容解析为新的XML文档,然后将新文档的根元素 导入(复制)到你的文档中:

String columnName = "XML_00805F49916B";
String valueString = "<IBS><Product></Product></IBS>";
Element node = doc.createElement(columnName);

Document valueDoc = builder.parse(
    new InputSource(new StringReader(valueString)));
Node valueElement = doc.importNode(valueDoc.getDocumentElement(), true);
node.appendChild(valueElement);

row.appendChild(node);
英文:

A text node is not part of an XML document’s structure. It’s just text. The special characters &lt;, &gt;, and &amp; are just characters when it comes to text nodes, though the final XML representation will need to escape them.

You cannot accomplish your goal by inserting a text node. You need to insert elements.

From your comment:

> Unfortunately I am getting that XML i.e. valueString from external source, and its schema would be different all times. I need a generic way of adding a XML string value into Element

You can parse arbitrary XML content into a new XML document, and then import (copy) the new document’s root element to your document:

String columnName = &quot;XML_00805F49916B&quot;;
String valueString = &quot;&lt;IBS&gt;&lt;Product&gt;&lt;/Product&gt;&lt;/IBS&gt;&quot;;
Element node = doc.createElement(columnName);

Document valueDoc = builder.parse(
    new InputSource(new StringReader(valueString)));
Node valueElement = doc.importNode(valueDoc.getDocumentElement(), true);
node.appendChild(valueElement);

row.appendChild(node);

huangapple
  • 本文由 发表于 2020年10月14日 19:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64352187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定