如何以编程方式更新和添加元素到XSD [英] How to Programmatically Update and Add Elements to an XSD

查看:143
本文介绍了如何以编程方式更新和添加元素到XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?xml version = 1.0encoding =UTF-8?> 
< xs:schema xmlns =com / company / commonxmlns:xs =http://www.w3.org/2001/XMLSchema
targetNamespace =com / company / common / elementFormDefault =qualified>
< xs:include schemaLocation =DerivedAttributes.xsd/>
< xs:element name =MyXSDtype =MyXSD/>
< xs:complexType name =Containter1>
< xs:sequence>
< xs:element name =element1type =element1minOccurs =0
maxOccurs =unbounded/>
< xs:element name =element2type =element2minOccurs =0
maxOccurs =unbounded/>
< / xs:sequence>
< / xs:complexType>
< xs:complexType name =Containter2>
< xs:sequence>
< xs:element name =element3type =Type1minOccurs =0/>
< xs:element name =element2type =Type2minOccurs =0/>
< / xs:sequence>
< / xs:complexType>
< / xs:schema>

如何编程和添加一个元素(name =element3type =element 3 minOccurs =0maxOccurs =unbounded)到容器1?



我已经看过DOM,Xerces,JAXB ...但是没有真正的右方式遍历XSD并附加一个元素。 Xerces似乎很有希望,但它的文档很少..



谢谢!

解决方案>

使用DOM执行以下操作:

  //解析文件并将其转换为DOM 
文件doc = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new InputSource(test.xml));

//使用xpath查找要添加到
的节点XPath xPath = XPathFactory.newInstance()。newXPath();
NodeList nodes =(NodeList)xPath.evaluate(/ schema / complexType [@ name = \Containter1\],
doc.getDocumentElement(),XPathConstants.NODESET);

//创建元素添加
org.w3c.dom.Element newElement = doc.createElement(xs:element);
newElement.setAttribute(type,element3);
//根据需要设置其他属性

nodes.item(0).appendChild(newElement);


//输出
TransformerFactory
.newInstance()
.newTransformer()
.transform(new DOMSource(doc.getDocumentElement )),新的StreamResult(System.out));

Java XML文档相当广泛,有很多教程和代码示例可供找到。请参阅将XML数据读入DOM Java:如何通过org.w3c.dom.document上的xpath字符串 Java DOM - 另外创建和添加新元素的元素以及打印一个org.w3c.dom.Document到stdout的最短路径是什么?,了解有关使用概念的更多详细信息。


I need to programatically update an an existing XSD in java that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="com/company/common" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="com/company/common/" elementFormDefault="qualified">
    <xs:include schemaLocation="DerivedAttributes.xsd" />
    <xs:element name="MyXSD" type="MyXSD" />
    <xs:complexType name="Containter1">
        <xs:sequence>
            <xs:element name="element1" type="element1" minOccurs="0"
                maxOccurs="unbounded" />
            <xs:element name="element2" type="element2" minOccurs="0"
                maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Containter2">
        <xs:sequence>
            <xs:element name="element3" type="Type1" minOccurs="0" />
            <xs:element name="element2" type="Type2" minOccurs="0" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

How can programatically and add an element that has (name="element3" type="element 3" minOccurs="0" maxOccurs="unbounded") to Container 1?

I've looked into DOM, Xerces, JAXB...but there is no really clear "right" way iterate though an XSD and append an element. Xerces seems promising but there is little documentation for it..

Thanks!

解决方案

Here's how to do it using DOM:

    // parse file and convert it to a DOM
    Document doc = DocumentBuilderFactory
            .newInstance()
            .newDocumentBuilder()
            .parse(new InputSource("test.xml"));

    // use xpath to find node to add to
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xPath.evaluate("/schema/complexType[@name=\"Containter1\"]",
            doc.getDocumentElement(), XPathConstants.NODESET);

    // create element to add
    org.w3c.dom.Element newElement = doc.createElement("xs:element");
    newElement.setAttribute("type", "element3");
    // set other attributes as appropriate

    nodes.item(0).appendChild(newElement);


    // output
    TransformerFactory
        .newInstance()
        .newTransformer()
        .transform(new DOMSource(doc.getDocumentElement()), new StreamResult(System.out));

The documentation on the Java XML is rather extensive and there are many tutorials and code examples to be found. See Reading XML Data into a DOM, Java: how to locate an element via xpath string on org.w3c.dom.document, Java DOM - Inserting an element, after another for creating and adding a new Element, and What is the shortest way to pretty print a org.w3c.dom.Document to stdout? for more detailed information on used concepts.

这篇关于如何以编程方式更新和添加元素到XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆