Parse XML documents using Java

Source: Internet
Author: User
Tags xml example

The above is my final running result.

In order to consolidate the learning in the future, we will paste it here for future reference.

1. Library. xml:

 

<? XML version = "1.0" encoding = "gb2312"?>

<Books>

<Book email = "zhangyupeng_good">

<Name> JAVA </Name>

<Price> 100.00 </price>

</Book>

</Books>

2. domparse. java: (write it in Notepad.) import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstream; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import javax. XML. parsers. parserconfigurationexception; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; import Org. XML. sax. saxexception; Public Class domparse {public domparse () {documentbuilderfactory domfac = documentbuilderfactory. newinstance (); try {documentbuilder dombuilder = domfac. newdocumentbuilder (); inputstream is = new fileinputstream ("D:/zhangxin XML video tutorial/XML example and question/Java parsing XML document/library. XML "); document DOC = dombuilder. parse (is); element root = Doc. getdocumentelement (); nodelist books = root. getchildnodes (); If (books! = NULL) {for (INT I = 0; I <books. getlength (); I ++) {node book = books. item (I); If (book. getnodetype () = node. element_node) {string email = book. getattributes (). getnameditem ("email "). getnodevalue (); system. out. println (email); For (node = book. getfirstchild (); node! = NULL; node = node. getnextsibling () {If (node. getnodetype () = node. element_node) {If (node. getnodename (). equals ("name") {string name = node. getnodevalue (); string name1 = node. getfirstchild (). getnodevalue (); system. out. println (name); system. out. println (name1);} If (node. getnodename (). equals ("price") {string price = node. getfirstchild (). getnodevalue (); system. out. println (price) ;}}}}} catch (parser Configurationexception e) {e. printstacktrace ();} catch (saxexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string ARGs []) {New domparse () ;}} 3. compile: javac domparse. java 4. Run: Java domparse library. XML to see the first result. You will see a null value, which is caused by the following two lines of code: string name = node. getnodevalue (); ---> output null value string name1 = node. getfirstchild (). getnodevalue (); ---------> output Java, that is, the value we need

 

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.