使用 JAXB 解组 XML

2022-01-19 00:00:00 xml java jaxb

我在这里解决了几乎所有与此主题相关的问题.但无法得到合适的解决方案.

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

我的问题如下:

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

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 :

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

这里我没有 xsd 来生成我的类.我该如何进行?请帮助我.

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

谢谢

推荐答案

下面是一种使用 JAXB (JSR-222) 实现:

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

项目

我们将使用以下类作为根对象并使用 @XmlRootElement 对其进行注释.@XmlRootElement 注释告诉 JAXB 如果要解组的文档中的根元素是 items,则应该实例化此类,您还可以指定不同的名称 @XmlRootElement(name="foo").

We will use the following class for the root object and annotate it with @XmlRootElement. The @XmlRootElement annotation 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").

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;
    }

}

物品

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

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 @XmlElement to do so. I used the @XmlElement annotation to do this in the Items class for the items property.

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;
    }

}

演示

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

相关文章