动态地使用JAXB读取XML

时间:2017-03-15 07:20:31

标签: java xml jaxb

我正在尝试使用JAXB动态提取各种XML的各个字段。一个例子如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<Entities TotalResults="1">
    <Entity Type="test-set-folder">
        <Fields>
            <Field Name="id">
                <Value>1760</Value> 
            </Field>
            <Field Name="ver-stamp">
                <Value>0</Value> 
            </Field>
            <Field Name="parent-id">
                <Value>109</Value> 
            </Field>
            <Field Name="last-modified">
                <Value>2017-02-24 15:50:36</Value> 
            </Field>
            <Field Name="hierarchical-path">
                <Value>AAAAAAABN</Value> 
            </Field>
            <Field Name="description">
                <Value /> 
            </Field>
            <Field Name="view-order" /> 
            <Field Name="name">
                <Value>ABCDEF</Value> 
            </Field>
            <Field Name="attachment">
                <Value /> 
            </Field>
            <Field Name="workflow">
                <Value /> 
            </Field>
        </Fields>
        <RelatedEntities /> 
    </Entity>
</Entities>

我希望动态地执行此操作,因为每个XML的字段都会更改。我的基本目标是捕捉每个领域,以方便我。例如,field.id应返回1760等。

我的Entity.java如下:

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRootElement(name = "Entities")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entity {

    @XmlAttribute
    int id;

    @XmlAnyAttribute
    Map<QName, String> otherAttributes;

    String name;

    @XmlAnyElement(lax=true)
    List<Object> otherElements;

}

调用Entity.java的代码如下:

import java.io.File;
import java.util.Map.Entry;
import javax.xml.bind.*;
import javax.xml.namespace.QName;

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("path\\to xml\\file");
        Entity entity  = (Entity) unmarshaller.unmarshal(xml);

        // Mapped XML Attribute
        System.out.println("entity.id");
        System.out.println("    " + entity.id);

        // Other XML Attributes
        System.out.println("entity.otherAttributes");
        for(Entry<QName, String> entry : entity.otherAttributes.entrySet()) {
            System.out.println("    " + entry);
        }

        // Mapped XML Element
        System.out.println("entity.name");
        System.out.println("    " + entity.name);

        // Other XML Elements
        System.out.println(entity.otherElements);
        for(Object object : entity.otherElements) {
            System.out.println("    " + object);
        }

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

}

我的困惑是XML是嵌套的,即第四层数据是我感兴趣的:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<Entities TotalResults="1">
    <Entity Type="test-set-folder">
        <Fields>
            <Field Name="id">
                <Value>1760</Value> 
            </Field>
            .
            .
            .

我对Entity类的代码可能有误,但有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我认为您必须查看以下示例,其中xml结构可能会有所不同,您仍然可以使用jaxB加载内容

XSD schema for xml file that can change for JAXB

让我们看看它是否适合您!