要解析 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;
}
}