[英]Not able to unmarshal xml file using JAXB
我做了以下
步驟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>
步驟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>
步驟3:創建了以下POJO(刪除了注釋)
軍團標志
@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;
}
}
部分
@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;
}
}
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;
}
}
步驟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();
}
在這里,解組將給出異常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)
我不明白問題到底出在哪里。
您是否檢查過以確保問題不在文件路徑中? 即此行:
File file = new File("//main//resources//tacticalSymbols.xml");
試試這個
File file = new File("tacticalSymbols.xml");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.