问题描述
这是我的 XML
<?xml version="1.0"?>
<gpx version="1.1" creator="Example" xmlns="http://www.Example.com/1" xmlns:football="https://www.Example-football.com/xsd/football-ext">
<Metadata>
<name>hello world</name>
<time>2018-04-26T12:32:52</time>
<extensions>
<sportsMeta>
<football:length>5080.3714454417996</football:length>
</sportsMeta>
</extensions>
</Metadata>
</gpx>
将此添加到 package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.Example.com/1",elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,attributeFormDefault = XmlNsForm.UNQUALIFIED,xmlns = {
@javax.xml.bind.annotation.XmlNs(prefix = "football",namespaceURI = "https://www.Example-football.com/xsd/football-ext"),@javax.xml.bind.annotation.XmlNs(prefix = "",namespaceURI = "https://www.Example-football.com/xsd/football-ext/1/1") })
package com.example.football.share.gpx;
import javax.xml.bind.annotation.XmlNsForm;
我能读懂姓名、时间。但无法读取guid,长度。
这里是 SportsMeta.java
@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name = "sportsMeta",propOrder = {
"length"
})
public class SportsMeta {
protected BigDecimal length;
public BigDecimal getLength() {
return length;
}
public void setLength(BigDecimal length) {
this.length = length;
}
}
如何从 XML 文件中读取长度信息。
解决方法
在您的 XML 输入中,您有以下部分:
<sportsMeta>
<football:length>5080.3714454417996</football:length>
</sportsMeta>
其中命名空间前缀 football
指的是命名空间 URI"https://www.Example-football.com/xsd/football-ext"
由 xmlns:football="https://www.Example-football.com/xsd/football-ext"
定义
在您的 XML 中。
要将此 XML 正确映射到 Java,您需要指定
length
类的 SportsMeta
属性的命名空间 URI。
您可以通过注释 length
属性来做到这一点
使用 @XmlElement
和 指定其中的命名空间。
(另见API documentation of XmlElement.namespace
。)
@XmlElement(namespace = "https://www.Example-football.com/xsd/football-ext")
protected BigDecimal length;