Java Operations Common API

Source: Internet
Author: User

1.xml parsing needs to import Dom4j-full.jar package

Saxreader sax = new Saxreader (); Create Saxreader

Document doc = sax.read ("Address" or new File ("Address")); Read an XML file

Element elemp = Doc.getrootelement (); Get root element

Here if you want to find all the elements below the root element such as:

list<element> list = Elemp.elements (); Create a collection element

map<string,string> map = hasmap<string> (); Set up a Map collection

for (Element cf:list) {

String key = Cf.getname (); Get tag element access as key value

String value = Cf.gettext ();//Gets the text element inside the tag as value

Map.put (Key,value); To deposit values into the map collection

}

How to get the specified element

As shown above, the root element has been obtained.

Element root = element.element ("name"); Gets the specified element

String GetText (); Gets the current text element

Gets the specified element attribute attribute (int index) method

Gets the current element index for index starting from 0

Attribute attr =element.attribute ("name Like name")

2. Creating an XML file

Document doc = Documenthelper.createdocument (); Create Document Object

Add a root element

Element root = doc.addelement ("name"); Name represents the child element names that you want to add

Append child elements

Element ele = root.addelement ("name"); Name represents the child element names that you want to add

Adds the attribute ID value to the newly appended child element to CP;

Ele.addattribute ("id", "CP");

Java Operations Common API

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.