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>

这里有带XML数据的字符串变量。那么在解析的时候会怎么做呢?

4

1 回答 1

1

要解析 XML 形式的字符串,请使用以下代码:

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

}

和 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;
}

    }
于 2012-10-19T09:56:09.373 回答