首页 > 解决方案 > 无法从具有某些属性 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>

我想IDPbxProfileTable标记 ie501502XML 文件中提取,如下所示:

输出: [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">标签获得以上输出。

标签: javaxmljava-8jaxb

解决方案


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 + "]";
    }
}

推荐阅读