简体   繁体   English

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

[英]Unable to extract field from XML element with some attribute JAVA

I have below code and XML file:我有以下代码和 XML 文件:

Main:主要的:

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());
    }

}

Config:配置:

@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: 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: 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>

I want to extract ID from PbxProfileTable tag ie 501 and 502 from XML file as shown below:我想从 XML 文件中的PbxProfileTable标记中提取ID501502 ,如下所示:

Output: [PBXProfileTable [id=501], PBXProfileTable [id=502]] Output: [PBXProfileTable [id=501], PBXProfileTable [id=502]]

This code works fine only if I remove xmlns="http://example.com/yang/isbc-sig" from each PbxProfileTable tag ie <PbxProfileTable xmlns="http://example.com/yang/isbc-sig"> in XML file.仅当我从每个PbxProfileTable标记中删除xmlns="http://example.com/yang/isbc-sig"<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">时,此代码才能正常工作在 XML 文件中。 However If I run with <PbxProfileTable xmlns="http://example.com/yang/isbc-sig"> I get null as an output.但是,如果我使用<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">运行,我会得到null作为 output。 Can anyone help how to get above output with <PbxProfileTable xmlns="http://example.com/yang/isbc-sig"> tag.谁能帮助如何使用<PbxProfileTable xmlns="http://example.com/yang/isbc-sig">标签超越 output。

if you use xml namespaces, you need to add them to your jaxb classes如果您使用 xml 命名空间,您需要将它们添加到您的 jaxb 类中

So in your Config class:所以在你的Config class 中:

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

The PBXProfileTable ID field is also in namespace " http://example.com/yang/isbc-sig " since it is contained in PBXProfileTable. PBXProfileTable ID 字段也在命名空间“ http://example.com/yang/isbc-sig ”中,因为它包含在 PBXProfileTable 中。 Updating the ID field as below should worked for me.如下更新 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 + "]";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM