manipulating XML Files with Java (DOM parsing)

Source: Internet
Author: User

  XML Extensible Markup Language (extensible Markup Language) is a software and hardware-independent transport tool.

  The role of XML:

(1) as a configuration file

(2) Simplified data sharing

(3) Simplified data transfer

  XML Dom Parsing method

Import the Dom4j.jar package in the Java project.

  (i) Read the XML file in Java

<1> Create a Saxreader object.

      Raxreader reader=new Raxreader ();

    <2> invokes the document read (file file) method of Raxreader , which gets the document object of the XML file (the DOM structure that contains the XML file).

      Document Doc=reader.read (new File ("config + +"));

      Note: Read has many overloaded methods and can also pass in the Stream object.

    <3> invokes the element Getrootelement () method of Document to get the root element of the XML.

      Element root=doc.getrootelement ();

    <4> after getting the root element, there are many element methods that can be used to constantly parse the DOM structure of the XML.

(1) Gets all child elements under the current element

        List<element> elements ()

      (2) Gets all child elements of the same name under the current element

        list<element> elements (String name)

(3) gets the child element of the specified name of the current element

        Element Element (String name)

(4) Gets the name of the current element

        String GetName ()

(5) Get the contents of the current element

        String GetText ()

String Gettexttrim ()

(6) Gets the content of the child element of the specified name under the current element

        String Elementtext (string name)

(7) Gets the properties of the current element

        Attribute Attribute (int index) //Gets the current element index property, starting with 0

        Attribute Attribute (String name) //Gets the property of the current element specified name

   Related methods of <5> attribute Attribute

(1) Gets the name of the current property

        String GetName ()

(2) Gets the value of the current property

        String GetValue ()

(ii) Writing XML files in Java

The Document object is created, then the DOM structure of the document is continuously made, and the Document object is finally written to the specified XML file

    <1> Create a Document object first, you must use documenthelper static method document Static createdocument () to create the Document object

      Document doc=documenthelper.createdocument ()

    The element addelement () of the <2> document adds the specified name root element to the Document object ( can only be called once because the root element has only one )

Element root=doc.addelement (String name)

<3> Subsequent, element has many methods for continuously filling the DOM structure of document

(1) Adds a child element of the specified name under the current element (the return value is a child element)

        Element addelement (String name)

(2) Add its contents to the current element (return value current element)

        Element AddText (String text)

(3) in the current element of the add-on property (the return value is the current element, you can continuously append its properties)

        Element AddAttribute (String name,string value)

<4> After the DOM structure is complete, the Document object is written to the XML file

(1) Create XmlWriter Advanced Streaming Object

        XMLWriter writer=new XMLWriter ();

Note: XMLWriter writer=new XMLWriter (outputstream os), can omit the second step

(2) Setting low-level output stream for XmlWriter

        FileOutputStream fos=new FileOutputStream ("New.xml");

Writer.setoutputstream (FOS);

(3) Writing the Document object to an XML file

        Wirter.write (DOC);

(4) Finally close the XmlWriter stream object

        Writer.close ();

manipulating XML Files with Java (DOM parsing)

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.