【发布时间】:2023-04-03 09:15:01
【问题描述】:
我刚开始学习 XML 和 JAXB,做了一些如何编组和解组数据的示例。但是,我仍然无法使用命名空间解析 XML。谁能帮我这个例子
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Uuid xmlns:ns2="com/mypackage/test">
<value>123456789</value>
</ns2:Uuid>
这是我创建的类
@XmlRootElement (name = "Uuid", namespace = "com/mypackage/test")
public class Uuid {
protected String value;
@XmlElement(namespace = "ns2")
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
获取空输出
Uuid uuid = null;
JAXBContext jbContext;
try {
jbContext = JAXBContext.newInstance(Uuid.class);
Unmarshaller jbUnmarshaller = jbContext.createUnmarshaller();
uuid = (Uuid) jbUnmarshaller.unmarshal(file);
System.out.println(uuid.getValue());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
谢谢
【问题讨论】:
标签: xml namespaces jaxb