从java程序向XSD添加根元素

从java程序向XSD添加根元素,java,dom,xsd,jaxb,sax,Java,Dom,Xsd,Jaxb,Sax,我看过一些文章,其中我们可以向XML中添加根元素,但我想在传入的XSD周围专门添加一个根复杂元素。我在这里看到两种情况,其中现有XSD根元素是命名类型或匿名类型: 因此,如果具有命名根元素的xsd: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://a.b/c/d" elementFormDefault="qualified"> <x

我看过一些文章,其中我们可以向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"/>

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文档,然后修改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");
最终字符串XS_URI=”http://www.w3.org/2001/XMLSchema";
FileInputStream是=新的FileInputStream(“xsd2.xsd”);
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.parse(is);//我们的DOM文档
XPath=XPathFactory.newInstance().newXPath();
//搜索所需xs:element(旧元素)
XPathExpression firstElementExpr=xpath.compile(“/*/*[local-name()='element'][1]”);
Element Element=(Element)firstElementExpr.evaluate(doc,XPathConstants.NODE);
//创建新元素,将其添加到文档对象模型
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);//xs:schema中的新xs:element
newElement.appendChild(newComplexType);//新xs:element中的新xs:complexType
newcommplexType.appendChild(newSequence);//新xs:complexType中的新xs:sequence
newElement.setAttribute(“名称”、“新根”);//添加名称属性
//请注意,这些操作的顺序可以更改,生成的DOM应该是相同的。
newSequence.appendChild(元素);//旧元素在新的xs:sequence中移动
if(element.hasAttribute(“类型”)){
//一切都已完成(旧元素已在新序列中移动)
}否则{
NodeList childNodes=element.getChildNodes();//旧元素中的节点。它还包含注释()、文本节点等

for(int i=0;iDOM代表文档对象模型。使用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文档,然后修改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");
最终字符串XS_URI=”http://www.w3.org/2001/XMLSchema";
FileInputStream是=新的FileInputStream(“xsd2.xsd”);
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.parse(is);//我们的DOM文档
XPath=XPathFactory.newInstance().newXPath();
//搜索所需xs:element(旧元素)
XPathExpression firstElementExpr=xpath.compile(“/*/*[local-name()='element'][1]”);
Element Element=(Element)firstElementExpr.evaluate(doc,XPathConstants.NODE);
//创建新元素,将其添加到文档对象模型
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);//xs:schema中的新xs:element
appendChild(newComplexType);//新xs:element中的新xs:complexType
newcommplexType.appendChild(newSequence);//新xs:complexType中的新xs:sequence
newElement.setAttribute(“name”、“newRoot”);//添加name属性
//请注意,这些操作的顺序可以更改,生成的DOM应该是相同的。
newSequence.appendChild(element);//旧元素在新xs:sequence中移动
if(element.hasAttribute(“类型”)){
//一切都已完成(旧元素已在新序列中移动)
}否则{
NodeList childNodes=element.getChildNodes();//旧元素中的节点。它还包含注释()、文本节点等

对于(int i=0;i您尝试过任何Java代码吗?@TimBiegeleisen,已经添加了代码,但是如果我添加了新的根元素,DOM是否会对现有XSD进行更改?我无法。没有类型。
没有类型。它不会自动使用类型
。但是,是的:XSD是XML,因此DOM可以解析、操作和保存它。我的答案帮助您解决了问题?您尝试过任何Java代码吗?@TimBiegeleisen,已经添加了代码,但是如果我添加一个新的根元素,DOM是否会对现有XSD进行更改?我无法做到。
没有类型。它不会自动使用类型
。但是,是的:XSD是XML,所以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");