简体   繁体   English

无法使用JAXB解组xml文件

[英]Not able to unmarshal xml file using JAXB

I have done the following 我做了以下

  1. Created a XML file by myself. 我自己创建了一个XML文件。
  2. Converted it to XSD using online tool 使用在线工具将其转换为XSD
  3. Copied the generated POJOs in my project 在我的项目中复制生成的POJO
  4. And tried creating Object using JAXB unmarshaller 并尝试使用JAXB解组器创建对象

Step 1: XML I created 步骤1:我创建的XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<corpsSymbol>
    <section sectionname="ARMOUR">
        <SIDCvalue sidcname="ArmdRegt" sidc="SFGPUCA----FING"></SIDCvalue>
        <SIDCvalue sidcname="ArmdRegtHQ" sidc="SFGPUCA---AFING"></SIDCvalue>
    </section>
    <section sectionname="ENGINEERS">
        <SIDCvalue sidcname="EngineersCompany" sidc="SFGPUCE----EING"></SIDCvalue>
        <SIDCvalue sidcname="EngineersCompanyHQ" sidc="SFGPUCE---AEING"></SIDCvalue>
    </section>
</corpsSymbol>

Step 2: Converted it to XSD 步骤2:将其转换为XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="SIDCvalue">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute type="xs:string" name="sidcname" use="optional"/>
          <xs:attribute type="xs:string" name="sidc" use="optional"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
  <xs:element name="section">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="SIDCvalue" maxOccurs="unbounded" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute type="xs:string" name="sectionname" use="optional"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="corpsSymbol">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="section" maxOccurs="unbounded" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute type="xs:string" name="name"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

Step 3: Following POJOs are created (Removed the comment) 步骤3:创建了以下POJO(删除了注释)

CorpsSymbol 军团标志

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "section"
})
@XmlRootElement(name = "corpsSymbol")
public class CorpsSymbol {

    protected List<Section> section;
    @XmlAttribute(name = "name")
    protected String name;

    public List<Section> getSection() {
        if (section == null) {
            section = new ArrayList<Section>();
        }
        return this.section;
    }
    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

}

Section 部分

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "sidCvalue"
})
@XmlRootElement(name = "section")
public class Section {

    @XmlElement(name = "SIDCvalue")
    protected List<SIDCvalue> sidCvalue;
    @XmlAttribute(name = "sectionname")
    protected String sectionname;

    public List<SIDCvalue> getSIDCvalue() {
        if (sidCvalue == null) {
            sidCvalue = new ArrayList<SIDCvalue>();
        }
        return this.sidCvalue;
    }
    public String getSectionname() {
        return sectionname;
    }

    public void setSectionname(String value) {
        this.sectionname = value;
    }

}

SIDCvalue SIDC值

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "value"
})
@XmlRootElement(name = "SIDCvalue")
public class SIDCvalue {

    @XmlValue
    protected String value;
    @XmlAttribute(name = "sidcname")
    protected String sidcname;
    @XmlAttribute(name = "sidc")
    protected String sidc;

    public String getValue() {
        return value;
    }

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

    public String getSidcname() {
        return sidcname;
    }

    public void setSidcname(String value) {
        this.sidcname = value;
    }

    public String getSidc() {
        return sidc;
    }

    public void setSidc(String value) {
        this.sidc = value;
    }
}

Step 4: Trying to unmarshal 步骤4:尝试解组

File file = new File("//main//resources//tacticalSymbols.xml"); 
        JAXBContext context;
        try {
            context = JAXBContext.newInstance(CorpsSymbol.class);

            Unmarshaller unmarshaller = context.createUnmarshaller();

            CorpsSymbol symbols = (CorpsSymbol) unmarshaller.unmarshal(file);

            System.out.println(symbols.getName());
        } catch (JAXBException e) {
            e.printStackTrace();
        }

Here, the unmarshalling is giving an exception javax.xml.bind.UnmarshalException - with linked exception: [java.net.UnknownHostException: main] at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:246) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189) 在这里,解组将给出异常javax.xml.bind.UnmarshalException-带有链接的异常:[java.net.UnknownHostException:main]位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0( com.sun.xml.internal.bind.v2上的UnmarshallerImpl.java:246)。 157)在javax.xml.bind.helpers.AbstractUnmars上的javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)在javax.xml.bind.helpers.AbstractUnmarshallerImpl.java:162) .unmarshal(AbstractUnmarshallerImpl.java:189)

I am not able to understand, where exactly is the problem. 我不明白问题到底出在哪里。

Have you checked to make sure that the problem isn't in the file path? 您是否检查过以确保问题不在文件路径中? Namely this line: 即此行:

File file = new File("//main//resources//tacticalSymbols.xml"); 

Try this instead 试试这个

File file = new File("tacticalSymbols.xml"); 

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

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