简体   繁体   English

动态地使用JAXB读取XML

[英]Reading an XML using JAXB dynamically

I am trying to extract the various fields of an various XMLs dynamically using JAXB. 我试图使用JAXB动态提取各种XML的各个字段。 An example of one is as follows: 一个例子如下:

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

I want to do this dynamically as the fields change for every XML. 我希望动态地执行此操作,因为每个XML的字段都会更改。 My basic aim is to capture each field as use it for my convenience. 我的基本目标是捕捉每个领域,以方便我。 For example, field.id should return 1760, etc. 例如, field.id应返回1760等。

My Entity.java is as follows: 我的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;

}

The code for calling Entity.java is as follows: 调用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);
    }

}

My confusion is that the XML is nested, ie the fourth layer of data is what I'm interested in: 我的困惑是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>
            .
            .
            .

My code for the Entity class is probably wrong, but can someone help me with how to go about it? 我的Entity类的代码可能是错误的,但有人可以帮我解决这个问题吗?

I think you must look at the below example where xml structure can vary and still you are able to load content using jaxB 我认为你必须看下面的例子,其中xml结构可以变化,你仍然可以使用jaxB加载内容

XSD schema for xml file that can change for JAXB 可以为JAXB更改的xml文件的XSD架构

Lets see if it works for you! 让我们看看它是否适合你!

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

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