Methods for calling XML in Java: Documentbuilderfactory

Source: Internet
Author: User

Here's how to use it:

(1) Get the factory instance of DOM parser

Documentbuilderfactory domfac=documentbuilderfactory.newinstance ();

(2) DOM parser obtained from Dom factory

Documentbuilder Dombuilder=domfac.newdocumentbuilder ();

(3) Convert the XML document to be parsed into an input stream, so that the DOM parser can parse it

InputStream is= New FileInputStream ("Test1.xml");

(4) parse the input stream of the XML document and get a document

Document Doc=dombuilder.parse (IS);

(5) Get the root node of the XML document

Element root=doc.getdocumentelement ();

(6) Get node's child node

NodeList books=root.getchildnodes ();

Java code
  1. Package Com.st.demo;
  2. import java.io.File;
  3. import Java.io.FileInputStream;
  4. import Java.io.InputStream;
  5. import Javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import Org.w3c.dom.Node;
  10. import org.w3c.dom.NodeList;
  11. Public class XmlReader {
  12. Public Static void Main (string[] args) {
  13. XmlReader reader = new XmlReader ();
  14. }
  15. Public XmlReader () {
  16. Documentbuilderfactory DOMFAC = Documentbuilderfactory.newinstance ();
  17. Try {
  18. Documentbuilder Dombuilder = Domfac.newdocumentbuilder ();
  19. InputStream is = new fileinputstream (new File ("D:/test1.xml"));
  20. Document doc = Dombuilder.parse (IS);
  21. Element root = Doc.getdocumentelement ();
  22. NodeList books = Root.getchildnodes ();
  23. if (books!=null) {
  24. for (int i = 0; i < books.getlength (); i++) {
  25. Node book = Books.item (i);
  26. if (Book.getnodetype () ==node.element_node) {
  27. (7) Get the attribute value of the node
  28. String email=book.getattributes (). getNamedItem ("email"). Getnodevalue ();
  29. SYSTEM.OUT.PRINTLN (email);
  30. Note that the properties of the node are also its child nodes. Its node type is also Node.element_node
  31. (8) Round-robin nodes
  32. for (Node node=book.getfirstchild (); node!=null; node=node.getnextsibling ()) {
  33. if (Node.getnodetype () ==node.element_node) {
  34. if (Node.getnodename (). Equals ("name")) {
  35. String Name=node.getnodevalue ();
  36. String name1=node.getfirstchild (). Getnodevalue ();
  37. SYSTEM.OUT.PRINTLN (name);
  38. System.out.println (NAME1);
  39. }
  40. if (Node.getnodename (). Equals ("price")) {
  41. String price=node.getfirstchild (). Getnodevalue ();
  42. SYSTEM.OUT.PRINTLN (price);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. } catch (Exception e) {
  50. TODO auto-generated Catch block
  51. E.printstacktrace ();
  52. }
  53. }
  54. }

XML code
    1. <?xml version= "1.0" encoding= "GB2312" standalone= "no"?>
    2. <books>
    3. <book email= "Zhoujunhui">
    4. <name>rjzjh</name>
    5. <price>jjjjjj</price>
    6. </book>
    7. </books>

Methods for calling XML in Java: Documentbuilderfactory

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.