英文:
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("Results");
doc.appendChild(results);
Element row = doc.createElement("Row");
results.appendChild(row);
String columnName = "XML_00805F49916B";
String valueString = "<IBS><Product></Product></IBS>";
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, "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);
The above code returns output:
SQL Return message: <Results><Row><XML_00805F49916B>&lt;IBS&gt;&lt;Product&gt;&lt;/Product&gt;&lt;/IBS&gt;</XML_00805F49916B></Row></Results>
But, I want the output to be:
SQL Return message: <Results><Row><XML_00805F49916B><IBS><Product></Product></IBS></XML_00805F49916B></Row></Results>
Please can I have some help how to get the required output?
答案1
得分: 0
一个文本节点不是XML文档结构的一部分。它只是文本。特殊字符 <
、>
和 &
在文本节点中只是字符,尽管最终的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 <
, >
, and &
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 = "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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论