Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回空对象的JAXB_Java_Xml_Jaxb - Fatal编程技术网

Java 返回空对象的JAXB

Java 返回空对象的JAXB,java,xml,jaxb,Java,Xml,Jaxb,我正在开发一个Java应用程序来读取由第三方应用程序生成的XML文件。我能够毫无问题地读取XML属性,但是当我尝试映射元素的值时,它返回一个空对象。我肯定我错过了一些简单的东西,但我一直没能弄明白 实体: import java.lang.reflect.Field; import java.util.List; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement;

我正在开发一个Java应用程序来读取由第三方应用程序生成的XML文件。我能够毫无问题地读取XML属性,但是当我尝试映射元素的值时,它返回一个空对象。我肯定我错过了一些简单的东西,但我一直没能弄明白

实体:

import java.lang.reflect.Field;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Entity")
public class Entity {

    String externalId;

    List<Attribute> attributes;

    @XmlAttribute(name="ExternalId")
    public String getExternalId() {
        return externalId;
    }

    public void setExternalId(String externalId) {
        this.externalId = externalId;
    }

    @XmlElementWrapper(name="Attributes")
    @XmlElement(name="Attribute")
    public List<Attribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }

    @Override
    public String toString() {
        Field[] fields = this.getClass().getDeclaredFields();
        String res = "";
        try {
            for (Field field : fields) {
                res += field.getName() + " : " + field.get(this) + "\n";
            }
        } catch (Exception e) {
            e.printStackTrace(); 
        }

        return res;
    }

}
输入:

<?xml version="1.0" encoding="utf-8"?>
<Entity Id="718803" ExternalId="10000" LongName="Entity LongName">
    <Attributes>
        <Attribute Id="100" Name="Name1" LongName="LongName1">
            <Values>
                <Value>
                    <![CDATA[Value1]]>
                </Value>
            </Values>
        </Attribute>
        <Attribute Id="200" Name="Name2" LongName="LongName2">
            <Values>
                <Value>
                    <![CDATA[Value2]]>
                </Value>
            </Values>
        </Attribute>
        <Attribute Id="300" Name="Name3" LongName="LongName3">
            <Values>
                <Value>
                    <![CDATA[Value3]]>
                </Value>
            </Values>
        </Attribute>
    </Attributes>
</Entity>
提前感谢你的帮助

import java.lang.reflect.Field;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

//import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.FIELD)
public class Value {

    String Value;

    public String getValue() {
        return Value;
    }

    public void setValue(String Value) {
        this.Value = Value;
    }

    @Override
    public String toString() {
        Field[] fields = this.getClass().getDeclaredFields();
        String res = "\n";
        try {
            for (Field field : fields) {
                res += field.getName() + " : " + field.get(this) + "\n";
            }
        } catch (Exception e) {
            e.printStackTrace(); 
        }

        return res;
    }

}
<?xml version="1.0" encoding="utf-8"?>
<Entity Id="718803" ExternalId="10000" LongName="Entity LongName">
    <Attributes>
        <Attribute Id="100" Name="Name1" LongName="LongName1">
            <Values>
                <Value>
                    <![CDATA[Value1]]>
                </Value>
            </Values>
        </Attribute>
        <Attribute Id="200" Name="Name2" LongName="LongName2">
            <Values>
                <Value>
                    <![CDATA[Value2]]>
                </Value>
            </Values>
        </Attribute>
        <Attribute Id="300" Name="Name3" LongName="LongName3">
            <Values>
                <Value>
                    <![CDATA[Value3]]>
                </Value>
            </Values>
        </Attribute>
    </Attributes>
</Entity>
------- XML to Object -----------

externalId : 10000
attributes : [
attributeName : Name1
attributeLongName : LongName1
values : [
Value : null
]
, 
attributeName : Name2
attributeLongName : LongName2
values : [
Value : null
]
, 
attributeName : Name3
attributeLongName : LongName3
values : [
Value : null
]
]