Question

how i can parse the XML using Jaxb.

  <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://www.xyz.in"><root><Del><NUMBER>13691991</NUMBER><PIECES>2</PIECES><SHEETNO>D1415/001005</SHEETNO></Del></root></string>
Was it helpful?

Solution

Try to create these classes:

The class Del

package jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name = "Del")
public class Del {
@XmlElement(name = "NUMBER")
private String number;
@XmlElement(name = "PIECES")
private String pieces;
@XmlElement(name = "SHEETNO")
private String sheetno;
public Del() {
    super();
}

public void setNumber(String number) {
    this.number = number;
}

public String getNumber() {
    return number;
}

public void setPieces(String pieces) {
    this.pieces = pieces;
}

public String getPieces() {
    return pieces;
}

public void setSheetno(String sheetno) {
    this.sheetno = sheetno;
}

public String getSheetno() {
    return sheetno;
}
}  

The class MyString

package jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name = "string")
public class MyString {
    @XmlElement(name = "root")
    private Root root;

    public MyString() {
        super();
    }

    public void setRoot(Root root) {
        this.root = root;
    }

    public Root getRoot() {
        return root;
    }
}   

The class Root

package jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name = "root")
public class Root {
    @XmlElement(name = "Del")
    private Del del;

    public Root() {
        super();
    }

    public void setDel(Del del) {
        this.del = del;
    }

    public Del getDel() {
        return del;
    }
}  

The file package-info.java

@XmlSchema( namespace="http://www.xyz.in",  
            xmlns={ @XmlNs( prefix = "", namespaceURI = "http://www.xyz.in")},
                elementFormDefault = XmlNsForm.QUALIFIED)

package jaxb;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

**The Main class to test **

package jaxb;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Main {

    public static void main(String[] args){
        try {
            String file = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 
            "<string xmlns=\"http://www.xyz.in\">\n" + 
            "   <root>\n" + 
            "       <Del>\n" + 
            "           <NUMBER>13691991</NUMBER>\n" + 
            "           <PIECES>2</PIECES>\n" + 
            "           <SHEETNO>D1415/001005</SHEETNO>\n" + 
            "       </Del>\n" + 
            "   </root>\n" + 
            "</string>";
            JAXBContext jaxbContext = JAXBContext.newInstance(MyString.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            StringReader reader = new StringReader(file);
            MyString myString = (MyString)jaxbUnmarshaller.unmarshal(reader);
            System.out.println(myString);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top