JAVA Files
import java.io.File; import javax.xml.parsers.*; import org.w3c.dom.*;
public class xmlreadertest { Public static void main (string[] args) throws Exception {Documentbuilder db = Documentbuilderfactory. newinstance (). Newdocumentbuilder ();Document document = Db.parse (new File ("Test.xml")); //Parse the file into document type in the project root directory Element root = Document.getdocumentelement ();NodeList list = Root.getelementsbytagname ("Dbstore"); //Get page element showelem(list); } Public static void Showelem (NodeList nl) {For (int i = 0; i < nl.getlength (); i++) {Node n = nl.item (i); //Get parent node //child nodes NodeList childlist = N.getchildnodes ();For (int x = 0; x < childlist.getlength (); + +) {Node Childnode = Childlist.item (x); //Determine if the value taken is part of the element and is intended to filter out if (childnode instanceof Element) { //Get the name of the child node String childnodename = Childnode.getnodename ();System. Out. Print ("node name:" + childnodename); //Get the value of the child node String childnodevalue = Childnode.gettextcontent ();System. Out. println ("node value:" + childnodevalue); } } } } }
XML file:
<? XML version="1.0" encoding="Utf-8"?> <test> <dbstore> <code>systemmessage</code> <title > title: System Message </title> <content> Summary: The project you are managing has not been dispatched new tasks </content> <sendercode>system</sendercode> <sendername> Sender: System </sendername> <receivercode>Test</receivercode> <receivername> recipient: Test </receivername> <State > Status: Read </states> <desc> content: You have managed a project that has not been dispatched for three days, please note the project Progress </desc> <type > types: SystemMessage</type > <sendtime> send time: 2015-8-19</sendtime> <tocode> reply </tocode> </dbstore> </Test>
From for notes (Wiz)
Java parsing xml simple version