java 使用 JAXB 解组 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11152046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:59:25  来源:igfitidea点击:

Unmarshalling XML using JAXB

javaxmljaxb

提问by vidhya

I went through almost all questions related to this topic here. But was not able to get a proper solution.

我在这里解决了几乎所有与此主题相关的问题。但未能得到适当的解决方案。

My issue is as follows:

我的问题如下:

I created a simple program to unmarshall an xml file for which i had a xsd. I was able to do that successfully. But if i am getting an xml without xsd, how can I get my attributes from that, if the xml looks something like this :

我创建了一个简单的程序来解组我有一个 xsd 的 xml 文件。我能够成功地做到这一点。但是如果我得到一个没有 xsd 的 xml,我如何从中获取我的属性,如果 xml 看起来像这样:

<items>
  <item>
    <code>12000</code>
    <name>Samsung  620</name>
    <price>9999</price>
  </item>
  <item>
    <code>15000</code>
    <name>NOKIA</name>
    <price>19999</price>
  </item>
  <item>
    <code>18000</code>
    <name>HTC 620</name>
    <price>29999</price>
  </item>
</items> 

Here I don't have an xsd to generate my classes. How can i proceed? Kindly help me.

在这里,我没有 xsd 来生成我的类。我该如何继续?请帮助我。

Thank You

谢谢

回答by bdoughan

Below is one way that you could map your use case with a JAXB (JSR-222)implementation:

以下是您可以使用JAXB (JSR-222)实现映射用例的一种方式:

Items

项目

We will use the following class for the root object and annotate it with @XmlRootElement. The @XmlRootElementannotation tells JAXB that this class should be instantiated if the root element in the document being unmarshalled is items, you can also specify a different name @XmlRootElement(name="foo").

我们将使用以下类作为根对象并用@XmlRootElement. 该@XmlRootElement注解告诉JAXB这一类应该被实例化,如果文件被解组是根元素items,你也可以指定不同的名称@XmlRootElement(name="foo")

package forum11152046;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Items {

    private List<Item> items;

    @XmlElement(name="item")
    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

}

Item

物品

In this example I created a class where all the property names correspond directly to the names in the XML document. This means there aren't any annotations that need to be added. If you need to override the default name you can use an annotation such as @XmlElementto do so. I used the @XmlElementannotation to do this in the Itemsclass for the itemsproperty.

在本例中,我创建了一个类,其中所有属性名称都直接对应于 XML 文档中的名称。这意味着不需要添加任何注释。如果您需要覆盖默认名称,您可以使用诸如此类的注释@XmlElement。我使用@XmlElement注释Itemsitems属性的类中执行此操作。

package forum11152046;

public class Item {

    private int code;
    private String name;
    private int price;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

Demo

演示

package forum11152046;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Items.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11152046/input.xml");
        Items items = (Items) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(items, System.out);
    }

}

input.xml/Output

输入.xml/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
    <item>
        <code>12000</code>
        <name>Samsung  620</name>
        <price>9999</price>
    </item>
    <item>
        <code>15000</code>
        <name>NOKIA</name>
        <price>19999</price>
    </item>
    <item>
        <code>18000</code>
        <name>HTC 620</name>
        <price>29999</price>
    </item>
</items>

回答by toniedzwiedz

If you want to stick with JAXB, you can either write an XML Schema Document on your own to validate such XML (it looks simple but it's just an instance, you need to find out what could change in these documente beforehand) or create a POJO with JAXB annotationsmatching these nodes. I'm afraid there's no other way. You still have to know well what the format allows.

如果你想坚持使用 JAXB,你可以自己编写一个 XML 模式文档来验证这样的 XML(它看起来很简单,但它只是一个实例,你需要事先找出这些文档中可能发生的变化)或创建一个 POJO使用与这些节点匹配的JAXB 注释。恐怕没有别的办法了。您仍然必须清楚地了解格式允许的内容。