import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; i... ...
import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class test { public static void main(String[] args) { String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<Resources><Method><Name>Query</Name><Items><Item><Value Type=\"arrayOfArrayOf_string\">" + "<Row><Data>00</Data><Data/><Data/></Row><Row><Data>XM</Data><Data>XB</Data><Data>ZJHM</Data>" + "</Row><Row><Data>張三</Data><Data>男</Data><Data>45-0685-469045-645</Data>" + "</Row></Value></Item></Items></Method></Resources>"; long begin = System.currentTimeMillis(); parse( xml ); long after = System.currentTimeMillis(); System.out.println("DOM用時"+(after-begin)+"毫秒"); } public static void parse(String protocolXML){ try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(protocolXML))); Element root = doc.getDocumentElement(); NodeList nodes = root.getChildNodes(); foreach(nodes,"張三"); }catch(Exception e){ e.printStackTrace(); } } public static void foreach(NodeList nodelist,String findtxt){ if(nodelist!=null){ for(int i=0;i<nodelist.getLength();i++){ Node node = nodelist.item(i); if(node.getFirstChild()!=null){ System.out.println(node.getNodeName()+"---"+node.getFirstChild().getNodeValue()); if(findtxt.equals(node.getFirstChild().getNodeValue())){ System.out.println(findtxt); break; } } foreach(node.getChildNodes(),findtxt); } } } }