[JAVA100 example]070, using Jdom parsing xml

Source: Internet
Author: User
Tags gettext tag name

Import org.jdom.*;


import org.jdom.input.*;


import org.jdom.output.*;


import java.io.*;


import java.util.List;





/**


* <p>title: Using jdom parsing xml</p>


* <p>description: parsing book.xml files </p>
by using the Jdom interface

* <p>copyright:copyright (c) 2003</p>


* <p>Filename:JDOMParsePage.java</p>


* @version 1.0


*/


public class Jdomparsepage {


/**


*<br> Method Description: Constructor, implement XML file parsing


*<br> input Parameters:


*<br> return type:


*/


public Jdomparsepage () {


Document Docjdom;


//using SAX to establish document


Saxbuilder bsax = new Saxbuilder (false);


try {


//Generate Document Object


Docjdom = Bsax.build (New File ("Book.xml"));


}catch (jdomexception e) {


E.printstacktrace ();


return;


  }


//Get root of document (node name: book)


Element root = docjdom.getrootelement ();


System.out.println ("root node Tag name:" + root.getname ());


System.out.println ("* * * * traverse XML elements");


//Get page element collection


list = Root.getchildren ("page");


//Traverse page element


for (int i=0 i < list.size (); i++) {


//Get page element


element element = (Element) list.get (i);


//Get id attribute


String id = element.getattributevalue ("id");


//Get TITLE element collection


List titlelist = Element.getchildren ("title");


//Get title first element


Element titleelement = (element) titlelist.get (0);


Gets the first value of the title element


String title = Titleelement.gettext ();


//Get the file element collection


List filelist = Element.getchildren ("file");


//Get file First element


Element fileelement = (element) filelist.get (0);


//Gets the first value of the file element


String file = Fileelement.gettext ();


System.out.println ("ID:" + ID + "" +


"title:" + title + "" +


"Files:" + file);


   }


 }


/**


*<br> Method Description: Main method, start parser


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] args) {


Jdomparsepage myreader = new Jdomparsepage ();


 }


}

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.