Java XML Framework dom4j parsing XML strings and using DOM4J to create XML

Source: Internet
Author: User

DOM4J is one of the most common XML parsing frameworks in Java, and the other jdom and Dom are excellent XML frameworks, taking dom4j as an example to illustrate XML string parsing


This is the structure of the XML in the code, MetaData has an array structure that needs to be read in a circular

<?xml version= "1.0"?>
<item name= "\" 20160202014140_208_59400.jpg\ "" id= "\" 9737853a-3cb8-48d5-a735-a75c3e8fa721\ "" Relativepath= "\" user\\obama\\20160202014140_208_59400.jpg\ "" >
    <fileinfo fileindexed= "\" 2016-02-02 "numpages=" \ "1\" "resolution=" \ "96\" "width=" \ "1600\" "height=" \ "1200\" " Filesize= "\" 202.42 ">
        <metaData>
            <item name=" \ "Width" value= "\" 1600\ "" "/> <item name=
            "\" Height "value=" \ "1200\" "/>
            <item name=" \ "resolution\" "value=" \ "96\" "/>
        </metaData>
        <boxes/>
    </fileInfo>
</item>


Code Demo

Package dom4j;
Import Org.dom4j.Attribute;
Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentHelper;
Import Org.dom4j.io.SAXReader;
Import Org.dom4j.jaxb.JAXBReader;
Import org.dom4j.Element;
Import java.awt.Component;

Import Java.util.Iterator;

Import java.util.List; public class Test01 {public static void main (string[] args) {//XML demo string string xml = "<item name=\" 201 60202014140_208_59400.jpg\ "id=\" 9737853a-3cb8-48d5-a735-a75c3e8fa721\ "relativepath=\" user\\Obama\\ 20160202014140_208_59400.jpg\ "><fileinfo fileindexed=\" 2016-02-02 01:41:32\ "numPages=\" 1\ "resolution=\" 96\ "Width=\" 1600\ "height=\" 1200\ "filesize=\" 202.42 kb\ "><metadata><item name=\" width (px) \ "Value=\" 1600\ "/><item name=\" Height (px) \ value=\ "1200\"/><item name=\ "resolution\" value=\ "96\"/></metadata
    
    ><boxes/></fileinfo></item> "; try {//download XML Document doc = DocumenthelpEr.parsetext (XML);

      root element element = Doc.getrootelement ();
      Node by root String name = Element.attributevalue ("name");
      String id = element.attributevalue ("id");

      String RelativePath = Element.attributevalue ("RelativePath");
      SYSTEM.OUT.PRINTLN (name);
      SYSTEM.OUT.PRINTLN (ID);

      System.out.println (RelativePath);

      Node By/item/fileinfo element = Element.element ("FileInfo");
      String fileindexed = Element.attributevalue ("fileindexed");
      String numpages = Element.attributevalue ("NumPages");

      String Resolution = Element.attributevalue ("Resolution");
      System.out.println (fileindexed);
      System.out.println (NUMPAGES);

      SYSTEM.OUT.PRINTLN (resolution);
      Node By/item/fileinfo/metadata element = Doc.getrootelement ();
      element = Element.element ("FileInfo");
      element = Element.element ("MetaData");

      Iterator it = Element.elementiterator (); All over the metaData.Item and print while (It.hasnext ()) {//Get Item element item = (Element) it.next ();
        String n = item.attributevalue ("name");

        String v = item.attributevalue ("value");
        System.out.print (n);
        System.out.print (":");
      System.out.println (v);
    } catch (Documentexception e) {e.printstacktrace ();
 }

  }

}


By dom4j parsing, the XML string displays the following results:

20160202014140_208_59400.jpg
9737853a-3cb8-48d5-a735-a75c3e8fa721
User\obama\20160202014140_208_59400.jpg
2016-02-02 01:41:32
1
96
Width (px): 1600
Height (px): 1200

resolution:96


Create the XML code as follows:

Package code02;
Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;

Import org.dom4j.Element; public class Makexml {public static void main (string[] args) {//Create XML object Document doc = documenthelper.cr
    Eatedocument ();

    Set the encoding doc.setxmlencoding ("Utf-8");

    Create a root node Element root = doc.addelement ("root");
    Create a node, add a property, set the value Element Elementa = root.addelement ("Elementa"). AddAttribute ("Color", "red");
    Elementa.settext ("This is Red");
    Element ELEMENTB = root.addelement ("Elementb"). AddAttribute ("Color", "blue");

    Elementa.settext ("This is Blue");
    Create a node, add a property, set the value Element elementA1 = elementa.addelement ("elementA1"). AddAttribute ("Color", "pink");
    Elementa1.settext ("This is Pink");
    Element elementA2 = elementa.addelement ("elementA2"). AddAttribute ("Color", "brown");
    Elementa2.settext ("This is brown");
    Element elementA3 = elementa.addelement ("ElementA3"). AddAttribute ("Color", "gray"); Elementa3.settext ("Thisis Gray ");
    Create a node, add a property, set the value Element elementB1 = elementb.addelement ("elementB1"). AddAttribute ("Color", "green");
    Element elementB2 = elementb.addelement ("elementB2"). AddAttribute ("Color", "orange");

    Element elementB3 = elementb.addelement ("ElementB3"). AddAttribute ("Color", "yellow");
    Output XML String strxml = Doc.asxml ();
  System.out.println (strxml);
 }

}


Output results:

<?xml version= "1.0" encoding= "Utf-8"?>
<root>
    <elementa color= "Red" >this is blue
        < ElementA1 color= "Pink" >this is pink</elementa1> <elementa2 color=
        ' Brown ' >this is brown</ elementa2>
        <elementa3 color= "Gray" >this is gray</elementa3>
    </elementA>
    < ELEMENTB color= "Blue" >
        <elementb1 color= "green"/> <elementb2 color=
        "Orange"/>
        < ElementB3 color= "Yellow"/>
    </elementB>
</root>



DOM4J's download Address: http://sourceforge.net/projects/dom4j/


Q Group Discussion 236201801

.


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.