JAXB 问题(一)

从XML读取数据时,找不到相应的标签类。

person.xsd文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0"
targetNamespace="http://xml.javainthebox.net"
xmlns:tns="http://xml.javainthebox.net"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:person"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="age" type="xs:int"/>
<xs:attribute name="sex" type="tns:sex"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="sex">
<xs:restriction base="xs:string">
<xs:enumeration value="MALE"/>
<xs:enumeration value="FEMALE"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

artists.xml文件内容如下:

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persons>
<person name="Neil Young" age="62" sex="MALE" />
<person name="Bob Dylan" age="67" sex="MALE" />
<person name="Joni Mitchell" age="65" sex="FEMALE" />
</persons>

UnmarshallerSample.java文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import net.javainthebox.xml.Persons;
import net.javainthebox.xml.Person;
public class UnmarshallerSample {
public UnmarshallerSample() {
try {
JAXBContext context
= JAXBContext.newInstance("net.javainthebox.xml");
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("artists.xml");
Object obj = unmarshaller.unmarshal(file);
Persons artists = (Persons)obj;
java.util.List<Person> p = artists.getPerson();
for (Person person: artists.getPerson()) {
System.out.printf("%s\tAge: %2d Sex: %s%n",
person.getName(),
person.getAge(),
person.getSex());
}
} catch (JAXBException ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
new UnmarshallerSample();
}
}

按顺序执行:

1
2
3
4
5
C:\jaxb>xjc persons.xsd
C:\jaxb>javac net\javainthebox\xml\ObjectFactory.java
C:\jaxb>javac net\javainthebox\xml\package-info.java
C:\jaxb>javac UnmarshallerSample.java
C:\jaxb>java UnmarshallerSample

执行完后,出现下面错误:

  没有预期的元素(URI:””、ローカル:”persons”)。预期的元素是{http://xml.javainthebox.net}person,{http://xml.javainthebox.net}persons

解决方法:

修改artists.xml文件:

修改前:

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persons>
<person name="Neil Young" age="62" sex="MALE" />
<person name="Bob Dylan" age="67" sex="MALE" />
<person name="Joni Mitchell" age="65" sex="FEMALE" />
</persons>

修改后:

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persons xmlns="http://xml.javainthebox.net" >
<person name="Neil Young" age="62" sex="MALE" />
<person name="Bob Dylan" age="67" sex="MALE" />
<person name="Joni Mitchell" age="65" sex="FEMALE" />
</persons>