使用jaxb(Unmarshal)将XML转换为Java对象

时间:2017-09-21 10:10:26

标签: java xml jaxb

我正在尝试将我的下面的xml转换为java对象。

这是我的xml:

<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:voc="urn:hl7-org:v3/voc">
  <confidentialityCode code="" codeSystem=""/>
  <languageCode code="en-"/>
  <recordTarget>
    <patientRole>
      <id root="" extension=""/>
      <telecom value="" use=""/>
      <providerOrganization>
        <id root="" extension=""/>
        <id root="" extension=""/>
        <name>Something</name>
        <telecom value=""/>
        <addr use="">
        <state></state>
        <city></city>
        <postalCode></postalCode>
        <streetAddressLine>2121</streetAddressLine>
        </addr>
      </providerOrganization>
    </patientRole>
  </recordTarget>
</ClinicalDocument>

我需要获得&#34; name&#34;的价值。在&#34; providerOrganization&#34;。 下面是我的Java类。

ClinicalDocument.java

package com.biclinical.data;
import java.util.List;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="ClinicalDocument", namespace="urn:hl7-org:v3")
public class ClinicalDocument {

    @XmlElement(name="recordTarget")
    private List<RecordTarget> recordTarget;

    public List<RecordTarget> getRecordTarget() {
        return recordTarget;
    }

    public void setRecordTarget(List<RecordTarget> recordTarget) {
        this.recordTarget = recordTarget;
    }

    @Override
    public String toString() {
        return "ClinicalDocument [recordTarget=" + recordTarget + "]";
    }
}

RecordTarget.java

package com.biclinical.data;

import java.util.List;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="recordTarget")
public class RecordTarget {

    @XmlElement(name="patientRole")
    private List<PatientRole> patientRole;

    public List<PatientRole> getPatientRole() {
        return patientRole;
    }

    public void setPatientRole(List<PatientRole> patientRole) {
        this.patientRole = patientRole;
    }

    @Override
    public String toString() {
        return "RecordTarget [patientRole=" + patientRole +"]";
    }
}

PatientRole.java

  package com.biclinical.data;

    import java.util.List;

    import javax.xml.bind.annotation.*;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "patientRole")
    public class PatientRole {

        /*@XmlElement(name = "id")
        private String id;
        Double root;
        String extension;*/

        @XmlElement(name="providerOrganization")
        private List<ProviderOrganization> providerOrganization;

        public List<ProviderOrganization> getProviderOrganization() {
            return providerOrganization;
        }

        public void setProviderOrganization(List<ProviderOrganization> providerOrganization) {
            this.providerOrganization = providerOrganization;
        }

    }

ProviderOrganisation.java

package com.biclinical.data;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="providerOrganization")
public class ProviderOrganization {

    @XmlElement(name="name")
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Patient [Name=" + name + "]";
    }
}

XMLFileParserSAXUtility.java

public class XMLFileParserSAXUtility extends DefaultHandler {

    public static void main(String[] args) {
        try {

            File file = new File("C:/Users/shivendras/Desktop/Patient19999_Test_Organization1.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {ClinicalDocument.class,RecordTarget.class,PatientRole.class,ProviderOrganization.class});
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            ClinicalDocument clinicalDocument = (ClinicalDocument) jaxbUnmarshaller.unmarshal(file);
            //clinicalDocument.getRecordTarget()
            String s = ((File) ((PatientRole) ((RecordTarget) clinicalDocument.getRecordTarget()).getPatientRole()).getProviderOrganization()).getName();
            System.out.println(s);
        } catch (JAXBException e) {
            e.printStackTrace();
          }
    }

我得到结果

Exception in thread "main" java.lang.NullPointerException
    at com.biclinical.util.XMLFileParserSAXUtility.main(XMLFileParserSAXUtility.java:27)

如果我尝试打印syso(clinicalDocument);

结果是ClinicalDocument [recordTarget = null]

请帮帮我吧!

2 个答案:

答案 0 :(得分:0)

我认为您将名称空间添加到@XmlElement

@XmlElement(name="patientRole")
private List<PatientRole> patientRole;

应该是:

@XmlElement(name="patientRole",namespace="urn:hl7-org:v3")
private List<PatientRole> patientRole;

如果对象中有任何其他null,请尝试添加命名空间。

此外,@XmlRootEntity仅对您的根元素是必需的,在本例中为您的ClinicalDocument类,您只需要将根类提供给您的JAXBContext:

JAXBContext jaxbContext = JAXBContext.newInstance(ClinicalDocument.class);

答案 1 :(得分:0)

您编写的以下代码不正确:

((File) ((PatientRole) ((RecordTarget) clinicalDocument.getRecordTarget()).getPatientRole()).getProviderOrganization()).getName()

如果你得到 clinicalDocument.getRecordTarget()并尝试投射它,它会抛出一个例外:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to RecordTarget

clinicalDocument.getRecordTarget()返回RecordTarget列表。所以你必须得到像下面这样的对象

if(!clinicalDocument.getRecordTarget().isEmpty() ||
                !clinicalDocument.getRecordTarget().get(0).getPatientRole().isEmpty() ||
                !clinicalDocument.getRecordTarget().get(0).getPatientRole().get(0).getProviderOrganization().isEmpty()) {
            System.out.println(clinicalDocument.getRecordTarget().get(0).getPatientRole().get(0).getProviderOrganization());
        }