There are many ways for processing XML in Java, personal tasks dom4j or better quality. Here are these simple to use
First supplement on import
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element;
1. First we will normally get a string or xml file format according to various ways, for example:
static String strxml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><rfc:ZFUN0022.Response xmlns:rfc=\"urn:sap-com:document:sap:rfc:functions\">"+
"<E_RETURN>1</E_RETURN><E_RETURNTEXT>读取成功</E_RETURNTEXT><PO_ATTACHMENT></PO_ATTACHMENT><PO_HEADER><BUTXT></BUTXT><DRART></DRART><PO_ITEM><item><Name></Name></item><item><Name></Name></item><item><Name></Name></item><item><Name></Name></item></PO_ITEM></rfc:ZFUN0022.Response>";
2. The first step, parse the string or file, get root
DOC = the Document null ; DOC = DocumentHelper.parseText (xml2); the Element rootElt = doc.getRootElement (); // get the root section
3. The root is also an element to take elements and their value should be at the root
// get the next node element
the Element po_item = rootElt.element ( "PO_ITEM");
// get the value of the element
String strValue = rootElt.elementTextTrim ( "PO_ITEM")
4. is a list of some elements, such values should be taken
= Po_item.elements items List ( "Item" ); for (= the Iterator Item items.iterator (); item.hasNext ();) { the Element ELM = (the Element) item.next (); // do something ... .. // value of the processing cycle for each list element System.out.println (elm.elementTextTrim ( "the Name" )); }
Simple basic types of data usage has been good enough.