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