简体   繁体   English

使用JAXB解组XML

[英]Unmarshalling XML using JAXB

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. 我创建了一个简单的程序来解组我有一个xsd的xml文件。 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,如果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. 在这里,我没有xsd来生成我的类。 How can i proceed? 我该怎么办? Kindly help me. 请帮助我。

Thank You 谢谢

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 . 我们将使用以下类作为根对象,并使用@XmlRootElement对其进行@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") . @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. 在这个例子中,我创建了一个类,其中所有属性名称都直接对应于XML文档中的名称。 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. 如果需要覆盖默认名称,可以使用注释(如@XmlElement来执行此操作。 I used the @XmlElement annotation to do this in the Items class for the items property. 我使用@XmlElement批注在items属性的Items类中执行此操作。

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

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 annotations matching these nodes. 如果你想坚持使用JAXB ,你可以自己编写一个XML Schema Document来验证这样的XML(它看起来很简单,但它只是一个实例,你需要事先找出这些文档中可能发生的变化)或者创建一个POJO使用与这些节点匹配的JAXB注释 I'm afraid there's no other way. 我担心别无他法。 You still have to know well what the format allows. 您仍然需要知道格式允许的内容。

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

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