Java parsing XML simple version

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.