无法从具有某些属性 JAVA 的 XML 元素中提取字段

Unable to extract field from XML element with some attribute JAVA

我有以下代码和 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 + "]";
    }

}

PBXProfileTable:

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>

我想从 PbxProfileTable 标签中提取 ID,即从 XML 文件中提取 501502,如下所示:

输出: [PBXProfileTable [id=501], PBXProfileTable [id=502]]

仅当我从每个 PbxProfileTable 标记中删除 xmlns="http://example.com/yang/isbc-sig" 时,此代码才能正常工作,即 XML 文件中的 <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"> 标签获得上述输出。

如果您使用 xml 命名空间,您需要将它们添加到您的 jaxb classes

所以在你的 Config class:

@XmlElement(name="PbxProfileTable", namespace="http://example.com/yang/isbc-sig")
public List<PBXProfileTable> getPbxprofileTable() {
    return pbxprofileTable;
}

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