gpt4 book ai didi

java - 从 java 程序将根元素添加到 XSD

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:49 26 4
gpt4 key购买 nike

我看过一些帖子,其中我们可以向 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 解析器可以用于上述两种情况吗?

那么我可以构造新的根元素,然后将旧的根元素嵌入其中吗?这是我如何解决这个问题还是有其他方法?

代码:

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));

最佳答案

DOM 代表文档对象模型。使用 DOM 文档,您正在使用包含文档模型(结构)的对象,您可以追加元素、删除元素等,模型(DOM 对象/文档的结构)将会更改,但这不会更改任何文件内容,除非您专门将 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 问题中看到的那样。

关于java - 从 java 程序将根元素添加到 XSD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37127885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com