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

时间:2017-07-12 16:05:55

标签: java xml xsd

我正在寻找一个可以采用XSD架构并创建一个可以操作的示例XML文档的Java库。在我的简单示例中,我获得了一个加载,操作和打印的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")));

1 个答案:

答案 0 :(得分:1)

一个。使用JAXB(首选):

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

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

  5. 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>