3
<string> 
    <Root>
        <Employee ID="1" PROJECTGROUP="Web" PROJECTKEY="1" PROJECT="ABC Project" DSCRIPTON="Logidtic Project" TOTALTASK="50" TOTALOPENTASK="50" TOTALCLOSETASK="0" HIGHPRIORITY="3"/>  
    </Root>
</string>

Here there is string variable with XML data. So while parsing how it will be done?

1 Answer 1

1

To parse a String in XML form use the below code:

String xml ="valid xml here";
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(is));
/* Parsing has finished. */ 

ExampleHandler.java:

class ExampleHandler extends DefaultHandler { 
StringBuffer buff = null;
boolean buffering = false; 
public static EmployeeVO empVO=null;

@Override
public void startElement(String namespaceURI, String localName, String qName, 
        Attributes atts) throws SAXException {
    if (localName.equals("Employee")) {
        empVO=new EmployeeVO();
        empVO.setEMPLOYEEID(atts.getValue("ID"));
        empVO.setPROJECTGROUP(atts.getValue("PROJECTGROUP"));
        empVO.setPROJECTKEY(atts.getValue("PROJECTKEY"));
        empVO.setPROJECT(atts.getValue("PROJECT"));
        empVO.setDSCRIPTON(atts.getValue("DSCRIPTON"));
        empVO.setTOTALTASK(atts.getValue("TOTALTASK"));
        empVO.setTOTALOPENTASK(atts.getValue("TOTALOPENTASK"));
        empVO.setTOTALCLOSETASK(atts.getValue("TOTALCLOSETASK"));
        empVO.setHIGHPRIORITY(atts.getValue("HIGHPRIORITY"));
    }   
} 


@Override
public void endElement(String namespaceURI, String localName, String qName) 
throws SAXException {

}

@Override
public void characters(char ch[], int start, int length) {
    if(buffering) {
        buff.append(ch, start, length);
    }
} 

}

and EmployeeVO.java

public class EmployeeVO {

String EMPLOYEEID;
String PROJECTGROUP;
String PROJECTKEY;
String PROJECT;
String DSCRIPTON;
String TOTALTASK;
String TOTALOPENTASK;
String TOTALCLOSETASK;
String HIGHPRIORITY;

public String getEMPLOYEEID() {
    return EMPLOYEEID;
}
public void setEMPLOYEEID(String eMPLOYEEID) {
    EMPLOYEEID = eMPLOYEEID;
}
public String getPROJECTGROUP() {
    return PROJECTGROUP;
}
public void setPROJECTGROUP(String pROJECTGROUP) {
    PROJECTGROUP = pROJECTGROUP;
}
public String getPROJECTKEY() {
    return PROJECTKEY;
}
public void setPROJECTKEY(String pROJECTKEY) {
    PROJECTKEY = pROJECTKEY;
}
public String getPROJECT() {
    return PROJECT;
}
public void setPROJECT(String pROJECT) {
    PROJECT = pROJECT;
}
public String getDSCRIPTON() {
    return DSCRIPTON;
}
public void setDSCRIPTON(String dSCRIPTON) {
    DSCRIPTON = dSCRIPTON;
}
public String getTOTALTASK() {
    return TOTALTASK;
}
public void setTOTALTASK(String tOTALTASK) {
    TOTALTASK = tOTALTASK;
}
public String getTOTALOPENTASK() {
    return TOTALOPENTASK;
}
public void setTOTALOPENTASK(String tOTALOPENTASK) {
    TOTALOPENTASK = tOTALOPENTASK;
}
public String getTOTALCLOSETASK() {
    return TOTALCLOSETASK;
}
public void setTOTALCLOSETASK(String tOTALCLOSETASK) {
    TOTALCLOSETASK = tOTALCLOSETASK;
}
public String getHIGHPRIORITY() {
    return HIGHPRIORITY;
}
public void setHIGHPRIORITY(String hIGHPRIORITY) {
    HIGHPRIORITY = hIGHPRIORITY;
}

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.