java - 无法从具有某些属性 JAVA 的 XML 元素中提取字段
问题描述
我有以下代码和 XML 文件:
主要的:
public class Main {
public static void main(String[] args) throws JAXBException {
File file = new File("etc/test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Config obj = (Config) jaxbUnmarshaller.unmarshal(file);
System.out.println(obj.getPbxprofileTable());
}
}
配置:
@XmlRootElement(name="config")
public class Config {
private List<PBXProfileTable> pbxprofileTable;
public Config() {}
public Config(List<PBXProfileTable> pbxprofileTable) {
super();
this.pbxprofileTable = pbxprofileTable;
}
@XmlElement(name="PbxProfileTable")
public List<PBXProfileTable> getPbxprofileTable() {
return pbxprofileTable;
}
public void setPbxprofileTable(List<PBXProfileTable> pbxprofileTable) {
this.pbxprofileTable = pbxprofileTable;
}
@Override
public String toString() {
return "Config [pbxprofileTable=" + pbxprofileTable + "]";
}
}
PBX 配置文件表:
public class PBXProfileTable {
private int id;
public PBXProfileTable() {}
public PBXProfileTable(int id) {
super();
this.id = id;
}
@XmlElement(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "PBXProfileTable [id=" + id + "]";
}
}
XML:
<config>
<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">
<ID>501</ID>
<NAME>ENELITALIASRL011</NAME>
<Record>
<PBX_RECORD_ID>2</PBX_RECORD_ID>
<PBX_NAME>ENELITALIASRL011</PBX_NAME>
<STATE>Enable</STATE>
<PBX_PRID>ENELITALIASRL011@wind.it</PBX_PRID>
<AUTH_SCHEME>No Authentication</AUTH_SCHEME>
<AUTH_DATA/>
<PBX_PUID_USER>WER011</PBX_PUID_USER>
<PBX_PUID_HOST>wind.it</PBX_PUID_HOST>
<REGISTRAR_NAME/>
<PBX_CONTACT_USER>WER011</PBX_CONTACT_USER>
</Record>
</PbxProfileTable>
<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">
<ID>502</ID>
<NAME>ENELITALIASRL011</NAME>
<Record>
<PBX_RECORD_ID>2</PBX_RECORD_ID>
<PBX_NAME>ENELITALIASRL011</PBX_NAME>
<STATE>Enable</STATE>
<PBX_PRID>ENELITALIASRL011@wind.it</PBX_PRID>
<AUTH_SCHEME>No Authentication</AUTH_SCHEME>
<AUTH_DATA/>
<PBX_PUID_USER>WER011</PBX_PUID_USER>
<PBX_PUID_HOST>wind.it</PBX_PUID_HOST>
<REGISTRAR_NAME/>
<PBX_CONTACT_USER>WER011</PBX_CONTACT_USER>
</Record>
</PbxProfileTable>
</config>
我想ID
从PbxProfileTable
标记 ie501
和502
XML 文件中提取,如下所示:
输出: [PBXProfileTable [id=501], PBXProfileTable [id=502]]
仅当我从XML 文件中xmlns="http://example.com/yang/isbc-sig"
的每个PbxProfileTable
标记中删除时,此代码才能正常工作。<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">
但是,如果我运行,<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">
我会得到null
输出。任何人都可以帮助如何使用<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">
标签获得以上输出。
解决方案
PBXProfileTable ID 字段也在命名空间“ http://example.com/yang/isbc-sig ”中,因为它包含在 PBXProfileTable 中。如下更新 ID 字段应该对我有用。
@XmlAccessorType(XmlAccessType.FIELD)
public class PBXProfileTable {
@XmlElement(name="ID" , namespace = "http://example.com/yang/isbc-sig")
private int id;
public PBXProfileTable() {}
public PBXProfileTable(int id) {
super();
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "PBXProfileTable [id=" + id + "]";
}
}
推荐阅读
- c - 使用 `poll()` 检查阻塞
- xamarin - Xamarin CameraX OnImageCapturedCallback
- xslt - 如何通过 xslt 1.0 删除 wsu:id 和命名空间声明
- react-native - 反应本机博览会快速图像不起作用
- python - 是否有 pyqt5 方法可以将 python 字符串转换为 QByteArray?
- html - 如何通过单击链接在模态中打开特定的可折叠面板
- python - 将请求库安装到目标文件夹
- vue.js - 以 vue 组件为 JS 对象构建步骤
- python - 将 SSML 和 Python 与 Azure 语音结合使用
- c# - 关于 tor selenium C#