提问人:Abhishek Singh 提问时间:6/12/2023 最后编辑:Abhishek Singh 更新时间:6/12/2023 访问量:38
在 Java 中将三个具有不同命名空间的 xml 文档合并为一个
Merge three xml document with different name spaces into one in Java
问:
我有 3 个不同的 xml 字符串(不是 JVM 中的文件字符串)。示例如下
一个 .xml
<A namespace="A..">
<Document>
<Header>...</Header>
<Body>
</Body>
</Document>
</A>
B.xml
<B namespace="B...">
<BusinessHeader>
....
</BusinessHeader>
</B>
C.xml
<C namespace="C..">
<BusinessBody>
</BusinessBody>
</C>
我希望 B 和 C 的内容与命名空间一起放在 A 的标签内。看<Body>
Result.xml
结果:.xml
<A namespace="A..">
<Document>
<Header>...</Header>
<Body>
<B namespace="B...">
<BusinessHeader>
....
</BusinessHeader>
</B>
<C namespace="C..">
<BusinessBody>
</BusinessBody>
</C>
</Body>
</Document>
</A>
我尝试这样做,但出现以下错误。
org.w3c.dom.Document a = convertStringToXml(transmissionReportPayload);
org.w3c.dom.Document b = convertStringToXml(headerPayLoad);
org.w3c.dom.Document c = convertStringToXml(documentPayload);
Node firstDocImportedNode = (Node) a.importNode(b, true);
a.appendChild(firstDocImportedNode );
StringWriter writer = new StringWriter();
// print results
TransformerFactory.newInstance()
.newTransformer()
.transform(new DOMSource(a), new StreamResult(writer));
return writer.toString();
我收到以下错误。
org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode(CoreDocumentImpl.java:1767)
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode(CoreDocumentImpl.java:1533)
at za.co.fnb.gts.iso.simulator.pacs002.Pacs002ResponseConverter.createXML(Pacs002ResponseConverter.java:89)
at za.co.fnb.gts.iso.simulator.pacs002.Pacs002ResponseConverter.convertPacs002ResponseToPayload(Pacs002ResponseConverter.java:73)
答:
0赞
vanje
6/12/2023
#1
您应该使用 document 元素而不是 document 。
也适用于目标元素。
这里有一个完整的示例,其中有正确的属性来表示默认命名空间。importNode()
xmlns="..."
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
public class ImportNode {
private static final String XML_A = "<A namespace=\"http://a.example.com\">\n" +
"<Document>\n" +
"<Header>...</Header>\n" +
"<Body>\n" +
"</Body>\n" +
"</Document>\n" +
"</A>";
private static final String XML_B = "<B xmlns=\"http://b.example.com\">\n" +
" <BusinessHeader>\n" +
" ....\n" +
" </BusinessHeader>\n" +
" </B>";
private static Document convertStringToXml(String xmlString) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setNamespaceAware(true);
StringReader reader = new StringReader(xmlString);
InputSource source = new InputSource(reader);
return fac.newDocumentBuilder().parse(source);
}
public static void main(String[] args) throws TransformerException, IOException, SAXException, ParserConfigurationException {
Document a = convertStringToXml(XML_A);
Document b = convertStringToXml(XML_B);
Node firstDocImportedNode = (Node) a.importNode(b.getDocumentElement(), true);
a.getDocumentElement().appendChild(firstDocImportedNode );
StringWriter writer = new StringWriter();
// print results
TransformerFactory.newInstance()
.newTransformer()
.transform(new DOMSource(a), new StreamResult(writer));
System.out.println(writer.toString());
}
}
输出为:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><A namespace="http://a.example.com">
<Document>
<Header>...</Header>
<Body>
</Body>
</Document>
<B xmlns="http://b.example.com">
<BusinessHeader>
....
</BusinessHeader>
</B></A>
评论
0赞
Abhishek Singh
6/13/2023
但是我想要文档元素中的 B
0赞
vanje
6/13/2023
然后,您应该稍微调整一下代码。首先找到文档 a 的 Document 元素。然后打电话给它。appendChild()
评论