Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

xml - Not able to extract only selected Tag values using Jaxb in Java

I've a very big XML string. Im posting here only part of the XML object. I'm trying to convert this XML to Java object. My first question is, should I need to create Java object of total XML tag values or can I have create the Java object of only selected inner XML objects? Please find my XML string.

<DATAPACKET REQUEST-ID = "2">
<HEADER>
    <RESPONSE-TYPE CODE="1" DESCRIPTION="Response DataPacket"/>
    <SEARCH-RESULT-LIST>
        <SEARCH-RESULT-ITEM>
            <NAME MATCHED="TRUE"/>              
        </SEARCH-RESULT-ITEM>
    </SEARCH-RESULT-LIST>
</HEADER>
<BODY>
    <CONS_SCORE>
        <SCORE>0</SCORE>
        <REASON1>
            <HEADER></HEADER>
            <DESCRIPTION></DESCRIPTION>
        </REASON1>
        <REASON2>
            <HEADER></HEADER>
            <DESCRIPTION></DESCRIPTION>
        </REASON2>
        <REASON3>
            <HEADER></HEADER>
            <DESCRIPTION></DESCRIPTION>
        </REASON3>
        <REASON4>
            <HEADER></HEADER>
            <DESCRIPTION></DESCRIPTION>
        </REASON4>
    </CONS_SCORE>
    <CONSUMER_PROFILE2>
        <CONSUMER_DETAILS2>
            <RUID>1234</RUID>
            <NAME>ABC</NAME>
            <DATE_OF_BIRTH>1980-03-03T00:00:00+03:00</DATE_OF_BIRTH>
            <GENDER>001</GENDER>                
        </CONSUMER_DETAILS2>
        <ID_DETAILS2>
            <SLNO>1</SLNO>
            <SOURCE_ID>001</SOURCE_ID>
            <ID_VALUE>2806</ID_VALUE>
            <EXP_DATE>2018-07-13T00:00:00+03:00</EXP_DATE>              
        </ID_DETAILS2>
    </CONSUMER_PROFILE2>
    </BODY>
</DATAPACKET>

In the above object, I want to fetch only CONSUMER_PROFILE2 object. Here is my Dto class

@XmlRootElement(name = "DATAPACKET")
public class ConsumerProfileDto {

private ConsumerDetailsDto CONSUMER_DETAILS2;

private IdDetailsDto ID_DETAILS2;

public ConsumerDetailsDto getCONSUMER_DETAILS2() {
    return CONSUMER_DETAILS2;
}

public void setCONSUMER_DETAILS2(ConsumerDetailsDto cONSUMER_DETAILS2) {
    CONSUMER_DETAILS2 = cONSUMER_DETAILS2;
}

public IdDetailsDto getID_DETAILS2() {
    return ID_DETAILS2;
}

public void setID_DETAILS2(IdDetailsDto iD_DETAILS2) {
    ID_DETAILS2 = iD_DETAILS2;
}
}

CONSUMER_DETAILS2 class

@XmlRootElement(name = "CONSUMER_DETAILS2")
public class ConsumerDetailsDto {

private String NAME;
private String DATE_OF_BIRTH;
private String GENDER;
private String NATIONALITY;
public String getNAME() {
    return NAME;
}
public void setNAME(String nAME) {
    NAME = nAME;
}
public String getDATE_OF_BIRTH() {
    return DATE_OF_BIRTH;
}
public void setDATE_OF_BIRTH(String dATE_OF_BIRTH) {
    DATE_OF_BIRTH = dATE_OF_BIRTH;
}
public String getGENDER() {
    return GENDER;
}
public void setGENDER(String gENDER) {
    GENDER = gENDER;
}
public String getNATIONALITY() {
    return NATIONALITY;
}
public void setNATIONALITY(String nATIONALITY) {
    NATIONALITY = nATIONALITY;
}

Here is the code of unmarshalling

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

    StringReader reader = new StringReader(responseXML);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
    ConsumerDetailsDto que= (ConsumerDetailsDto) jaxbUnmarshaller.unmarshal(reader);  

    System.out.println(que.getDATE_OF_BIRTH());  
    System.out.println(que.getGENDER());;
    System.out.println(que.getNAME());

P.S

I've used DocumentBuilder and I'm able to fetch the values using it. But, I want to extract using Jaxb.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't annotate as @XmlRootElement the Java class mirroring the XML element that you're interested on. You have to mirror in Java all the XML elements from the topmost element in the XML file (DATAPACKET) to the lowest ones (CONSUMER_DETAILS2 and ID_DETAILS2) in the path that you're interested, even if you're only interested in some of them.

I assume that the operator isn't interested in RUID (because the class CONSUMER_DETAILS2 doesn't include a field for this XML element), and also that the field NATIONALITY is not going to be loaded from this XML.

If the input XML of the question were in a file named input2.xml, then I'd have the following Java classes:

DataPacket

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "DATAPACKET")
public class DataPacket {

    @XmlAttribute(name = "REQUEST_ID")
    private int REQUEST_ID;

    @XmlElement(name ="BODY")
    private Body BODY;

    public DataPacket(){}

    public int getREQUEST_ID() {
        return REQUEST_ID;
    }

    public void setREQUEST_ID(int REQUEST_ID) {
        this.REQUEST_ID = REQUEST_ID;
    }

    public Body getBODY() {
        return BODY;
    }

    public void setBODY(Body BODY) {
        this.BODY = BODY;
    }
}

Body

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.NONE)
class Body {

    @XmlElement(name = "CONSUMER_PROFILE2")
    private ConsumerProfile profile;

    public Body(){}

    public ConsumerProfile getProfile() {
        return profile;
    }

    public void setProfile(ConsumerProfile profile) {
        this.profile = profile;
    }
}

ConsumerProfile

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.NONE)
public class ConsumerProfile {

    @XmlElement(name = "CONSUMER_DETAILS2")
    private ConsumerDetails CONSUMER_DETAILS2;

    @XmlElement(name = "ID_DETAILS2")
    private IdDetails ID_DETAILS2;

    public ConsumerProfile(){}

    public ConsumerDetails getCONSUMER_DETAILS2() {
        return CONSUMER_DETAILS2;
    }

    public void setCONSUMER_DETAILS2(ConsumerDetails cONSUMER_DETAILS2) {
        CONSUMER_DETAILS2 = cONSUMER_DETAILS2;
    }

    public IdDetails getID_DETAILS2() {
        return ID_DETAILS2;
    }

    public void setID_DETAILS2(IdDetails iD_DETAILS2) {
        ID_DETAILS2 = iD_DETAILS2;
    }
}

ConsumerDetails

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.NONE)
class ConsumerDetails {

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

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

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

    private String NATIONALITY;

    public ConsumerDetails(){}

    public String getNAME() {
        return NAME;
    }
    public void setNAME(String nAME) {
        NAME = nAME;
    }
    public String getDATE_OF_BIRTH() {
        return DATE_OF_BIRTH;
    }
    public void setDATE_OF_BIRTH(String dATE_OF_BIRTH) {
        DATE_OF_BIRTH = dATE_OF_BIRTH;
    }
    public String getGENDER() {
        return GENDER;
    }
    public void setGENDER(String gENDER) {
        GENDER = gENDER;
    }
    public String getNATIONALITY() {
        return NATIONALITY;
    }
    public void setNATIONALITY(String nATIONALITY) {
        NATIONALITY = nATIONALITY;
    }
}

IdDetails

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.NONE)
class IdDetails {

    @XmlElement(name="SLNO")
    private int SLNO;

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

    @XmlElement(name="ID_VALUE")
    private int ID_VALUE;

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

    public IdDetails(){}

    public int getSLNO() {
        return SLNO;
    }

    public void setSLNO(int SLNO) {
        this.SLNO = SLNO;
    }

    public String getSOURCE_ID() {
        return SOURCE_ID;
    }

    public void setSOURCE_ID(String SOURCE_ID) {
        this.SOURCE_ID = SOURCE_ID;
    }

    public int getID_VALUE() {
        return ID_VALUE;
    }

    public void setID_VALUE(int ID_VALUE) {
        this.ID_VALUE = ID_VALUE;
    }

    public String getEXP_DATE() {
        return EXP_DATE;
    }

    public void setEXP_DATE(String EXP_DATE) {
        this.EXP_DATE = EXP_DATE;
    }
}

Also, inside the same "test" package the jaxb.index file with:

DataPacket
ConsumerProfile
ConsumerDetails
IdDetails

Then, when testing with this Test class:

package test;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Test {

    public static void main(String[] args) {
        try{
            JAXBContext jc = JAXBContext.newInstance(DataPacket.class);

            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("input2.xml");
            DataPacket dp = (DataPacket) unmarshaller.unmarshal(xml);
            Body body = dp.getBODY();
            ConsumerProfile profile = body.getProfile();
            ConsumerDetails consumerDetail = profile.getCONSUMER_DETAILS2();
            IdDetails idDetails = profile.getID_DETAILS2();
            System.out.println("ConsumerDetails name:"+consumerDetail.getNAME()+
                               " date of birth:"+consumerDetail.getDATE_OF_BIRTH()+
                               " gender:"+consumerDetail.getGENDER()+
                               "IdDetails SLNO:"+idDetails.getSLNO()+
                                " SOURCE_ID:"+idDetails.getSOURCE_ID()+
                                " ID_VALUE:"+idDetails.getID_VALUE()+
                                " EXP_DATE:"+idDetails.getEXP_DATE());
        }
        catch(JAXBException e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...