我做了以下
第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(删除了评论)
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;
}
}
部分
@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
@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] 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) 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157) 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162) 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171) 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
我无法理解,问题究竟在哪里。
答案 0 :(得分:0)
您是否检查过以确保问题不在文件路径中?即这一行:
File file = new File("//main//resources//tacticalSymbols.xml");
试试这个
File file = new File("tacticalSymbols.xml");