使用JAXB解组XML [英] Unmarshalling XML using JAXB

查看:115
本文介绍了使用JAXB解组XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

项目

我们将使用以下root对象的类,并使用 @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 )来执行此操作。我使用 @XmlElement 注释在类中为项目执行此操作 property。

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 /输出

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

这篇关于使用JAXB解组XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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