Java method for reading XML files

Source: Internet
Author: User

I. Java class

  1. PackageCom. java. test;
  2.  
  3. ImportOrg. W3C. Dom .*;
  4. ImportJavax. xml. parsers .*;
  5. ImportJava. Io .*;
  6.  
  7. Public ClassJavareadxml {
  8. // Document can be seen as an image of XML in memory. Once this document is obtained
     
  9. // Memory operations to perform XML operations. First, obtain the XML-related document
     
  10. PrivateDocument Doc =Null;
  11.  
  12. Public VoidInit (string xmlfile)ThrowsException {
  13. // It is obvious that this class is a singleton. Obtain and generate the documentbuilder factory first.
     
  14. // The factory where a documentbuilder is generated,
     
  15. // Documentbuilder is used to generate document
     
  16. Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
  17. Documentbuilder DB = DBF. newdocumentbuilder ();
  18. // This document is an image of the XML file in the memory.
     
  19. Doc = dB. parse (NewFile (xmlfile ));
  20. }
  21.  
  22. // This method is used to display the content of the XML file.
     
  23. Public VoidViewxml (string xmlfile)ThrowsException {
  24. This. INIT (xmlfile );
  25. // There is only one root element in the XML file. Take the root element out first.
     
  26. Element element = Doc. getdocumentelement ();
  27. System. Out. println ("root element:" + element. gettagname ());
  28.  
  29. Nodelist = Doc. getelementsbytagname ("person ");
  30. System. Out. println ("book node chain length:" + nodelist. getlength ());
  31.  
  32. Node fathernode = nodelist. Item (0 );
  33. System. Out. println ("parent node:" + fathernode. getnodename ());
  34.  
  35. // Take out the attributes of the parent node
     
  36. Namednodemap attributes = fathernode. getattributes ();
  37.  
  38. For(IntI = 0; I <attributes. getlength (); I ++ ){
  39. Node attribute = attributes. item (I );
  40. System. Out. println ("book attribute name:" + attribute. getnodename ()
  41. + "The corresponding property value is:" + attribute. getnodevalue ());
  42. }
  43.  
  44. Nodelist childnodes = fathernode. getchildnodes ();
  45. System. Out. println (childnodes. getlength ());
  46. For(IntJ = 0; j <childnodes. getlength (); j ++ ){
  47. Node childnode = childnodes. Item (j );
  48. // If the node belongs to the element, set the value.
     
  49. If(ChildnodeInstanceofElement ){
  50. // System. Out. println ("sub-node name:" + childnode. getnodename () + "the corresponding value is" + childnode. getfirstchild (). getnodevalue ());
     
  51. System. Out. println ("subnode name:" + childnode. getnodename ()
  52. + "The corresponding value is" + childnode. getfirstchild (). getnodevalue ());
  53. }
  54. }
  55.  
  56. }
  57.  
  58. Public Static VoidMain (string [] ARGs)ThrowsException {
  59. Javareadxml parse =NewJavareadxml ();
  60.  
  61. // My XML file
     
  62. Parse. viewxml ("person. xml ");
  63. }
  64. }

Ii. xml file

 
 
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Book>
  3. <Person>
  4. <First> Wang </first>
  5. <Last> laohu </last>
  6. <Age> 25 </age>
  7. <Version> China Post and Telecommunications Publishing House </version>
  8. </Person>
  9. <Person>
  10. <First> LI </first>
  11. <Last> junjia </last>
  12. <Age> 24 </age>
  13. <Version> Tsinghua University Press </version>
  14. </Person>
  15. </Book>

Iii. output results

The root element is book.
The length of the book node chain: 2
Parent node: person
9
Subnode name: the value corresponding to first is Wang
Child node name: the value corresponding to last is laohu
Subnode name: the value corresponding to age is 25.
Sub-node name: the value corresponding to version is China Post and Telecommunications Press
Source: http://developer.51cto.com/art/200906/128418.htm

Related Article

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.