繁体   English   中英

动态地使用JAXB读取XML

[英]Reading an XML using JAXB dynamically

我试图使用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类的代码可能是错误的,但有人可以帮我解决这个问题吗?

我认为你必须看下面的例子,其中xml结构可以变化,你仍然可以使用jaxB加载内容

可以为JAXB更改的xml文件的XSD架构

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

暂无
暂无

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

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