繁体   English   中英

在Java中使用XSD创建XML文档的实例

[英]Create instance of XML document using XSD in Java

我正在寻找一个Java库,该库可以采用XSD架构并创建一个示例XML文档,然后可以对其进行操作。 在我的简单示例中,为我提供了一个XML,它可以加载,操作和打印。 如果仅获得XSD,如何获得相同的结果?

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
                "<ns0:store_msg xmlns:ns0=\"http://data\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://data sample.xsd \">" + 
                "<item>" +
                "<name>A</name>" +
                "<price>1.1</price>" +
                "</item>" +
                "<item>" +
                "<name>B</name>" +
                "<price>2.2</price>" +
                "</item>" +
                "</ns0:store_msg>";

    // load XML
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));


    // manipulate XML
    Node root = doc.getFirstChild();
    Element el = doc.createElement("item");
    Element name = doc.createElement("name");
    Element price = doc.createElement("price");
    name.setTextContent("C");
    price.setTextContent("2.2");
    el.appendChild(name);
    el.appendChild(price);
    root.appendChild(el);

    // print XML
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), 
         new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));

A.使用JAXB(首选):

  1. 预先步骤:例如,通过maven-jaxb2-plugin或与其他工具或手动调用XJC编译器,从XSD生成JAXB Java类

  2. 需要类时,请创建您的根元素:

    StoreMsg myStoreMsgRoot = new StoreMsg(); // Class must be what JAXB generated for store_msg element

  3. 根据需要填充其中的所需元素。 例如:

      Item firstItem = new Item(); firstItem.setName("A"); firstItem.setPrice(1.1); myStoreMsgRoot.getItem().add(firstItem); 

    等等

  4. 然后使用JAXBContext将您的根元素编组为String

    JAXBContext jaxbCtx = JAXBContext.newInstance(StoreMsg.class);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); jaxbCtx.createMarshaller().marshal(myStoreMsgRoot, bos); String generatedXml = new String(bos.toByteArray()); bos.close();

B.有一个框架可以做到这一点-jlibs

一个简单的例子在这里: jlibs-XSInstance.wiki

除此之外,您可能需要设置一些属性来处理@VCR评论的情况。

乍一看,它以某种方式起作用。

只是最新版本需要依赖项(通过下面的Maven或类路径中的jar文件):

    <!-- https://mvnrepository.com/artifact/in.jlibs/jlibs-xsd -->
    <dependency>
        <groupId>in.jlibs</groupId>
        <artifactId>jlibs-xsd</artifactId>
        <version>2.2.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/in.jlibs/jlibs-xml -->
    <dependency>
        <groupId>in.jlibs</groupId>
        <artifactId>jlibs-xml</artifactId>
        <version>2.1</version>
    </dependency>

    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xerces</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.11.0-22</version>
    </dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM