我已经看过哪些帖子可以在哪里添加根元素到XML,但我想在传入的XSD周围添加根复合元素。我在这里看到两个案例,其中现有的XSD根元素是命名类型或匿名:
所以如果xsd带有命名的根元素:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://a.b/c/d" elementFormDefault="qualified">
<xs:element name="oldRoot" type="oldRoot"/>
应转换为:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://a.b/c/d" elementFormDefault="qualified">
<xs:element name="newRoot">
<xs:complexType>
<xs:sequence>
<xs:element name="oldRoot" type="oldRoot"/>
</xs:sequence>
</xs:complexType>
</xs:element>
和具有匿名类型的XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://a.b/c/d" elementFormDefault="qualified">
<xs:element name="oldRoot">
<xs:complexType>
<xs:sequence>
<xs:element name="oldChild1">
<xs:complexType>
<xs:sequence>
<xs:element name="someElement" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
应转换为
<xs:element name="newRoot">
<xs:complexType>
<xs:sequence>
<xs:element name="oldRoot"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="oldRoot">
<xs:sequence>
<xs:element name="oldChild1">
<xs:complexType>
<xs:sequence>
<xs:element name="someElement" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
我正在使用DOM解析器来帮助我进行这些编辑,但是DOM解析器可以用于上述两种情况吗?
那么我可以构造新的根元素然后将旧的Root元素嵌入其中吗?我是如何解决这个问题还是有其他办法?
代码:
is = new FileInputStream("/home/xyz/testing.xsd");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document oldDoc = builder.parse(is);
Node oldRoot = oldDoc.getDocumentElement();
Document newDoc = builder.newDocument();
Element newComplexType = newDoc
.createElement("xs:complexType");
Element newSequence = newDoc.createElement("xs:sequence");
Element newElement = newDoc.createElement("xs:element");
newComplexType.setAttribute("name", "newRoot");
newDoc.appendChild(newComplexType);
newComplexType.appendChild(newDoc.importNode(oldRoot, true));
答案 0 :(得分:0)
DOM代表文档对象模型。使用DOM文档,您正在使用包含文档的模型(结构)的对象,您可以追加元素,删除元素等,并且模型(DOM对象/文档的结构)将会发生变化,但这并没有改变。 t更改任何文件内容,除非您专门将DOM写入文件。
这意味着您不需要创建两个文档。您应该只从文件中创建一个DOM文档,然后修改DOM(移动和创建节点)并将DOM内容写入文件。
final String XS_URI = "http://www.w3.org/2001/XMLSchema";
FileInputStream is = new FileInputStream("xsd2.xsd");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is); // Our DOM document
XPath xpath = XPathFactory.newInstance().newXPath();
// Search required xs:element (old element)
XPathExpression firstElementExpr = xpath.compile("/*/*[local-name()='element'][1]");
Element element = (Element) firstElementExpr.evaluate(doc, XPathConstants.NODE);
// Create new elements, add them to Document Object Model
Element newElement = doc.createElementNS(XS_URI, "element");
Element newComplexType = doc.createElementNS(XS_URI, "complexType");
Element newSequence = doc.createElementNS(XS_URI, "sequence");
doc.getDocumentElement().appendChild(newElement); // new xs:element inside xs:schema
newElement.appendChild(newComplexType); // new xs:complexType inside new xs:element
newComplexType.appendChild(newSequence); // new xs:sequence inside new xs:complexType
newElement.setAttribute("name", "newRoot"); // Add name attribute
// Note that the order of these operations can be changed, the resulting DOM should be the same.
newSequence.appendChild(element); // The old element is moved inside the new xs:sequence
if (element.hasAttribute("type")) {
// Everything is done (the old element have been moved inside the new sequence)
} else {
NodeList childNodes = element.getChildNodes(); // Nodes inside old element. This also contains comments (<!-- -->), text nodes, etc
for (int i=0; i<childNodes.getLength(); i++) {
/*
* We iterate nodes till we found xs:complexType
* Then we move xs:comlpexType inside xs:schema and add the required name attribute
*/
if ("complexType".equals(childNodes.item(i).getLocalName())) {
Element complexType = (Element) childNodes.item(i);
complexType.setAttribute("name", "oldRoot"); // Maybe in your specific case, attribute value should be element.getAttribute("name")??
doc.getDocumentElement().appendChild(complexType);
}
}
}
saveDocument(doc, "newXSD.xsd");
最后,您可以将DOM文档编写到文件中,或者像许多Stackoverflow问题中看到的那样将其编写为String。