【发布时间】:2018-05-23 09:26:34
【问题描述】:
我做了以下事情
- 自己创建了一个 XML 文件。
- 使用在线工具将其转换为 XSD
- 将生成的 POJO 复制到我的项目中
- 并尝试使用 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:主要] 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:246) 在 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)
我无法理解,问题到底出在哪里。
【问题讨论】:
-
从您的 XML 中删除 doctype 声明(带有
<!DOCTYPE ...>的行)。 -
完成。还是一样的问题
-
你还记得你是否发现了这个问题吗?我现在遇到了类似的事情。
标签: java xml xsd jaxb unmarshalling