When flare reads an XML file in the external graphml format, it involves using doc4j.

Source: Internet
Author: User

To use dom4j to read and write XML documents, you need to first download the dom4j package, dom4j official website in http://www.dom4j.org/

 

The latest dom4j package:

Http://nchc.dl.sourceforge.net/sourceforge/dom4j/dom4j-1.6.1.zip

 

There are two packages after the unzipping. If you only operate the XML file, run dom4j- 1.6.1 . Jar can be added to the project, if you need to use XPath, you also need to add the package jaxen-1.1-beta-7.jar. bitscn_com

 

The following are related operations: bitscn_com

 

1. Document Object-related network management network bitscn.com

 

1. Read the XML file and obtain the document object.

 

Network Management Network bitscn_com

 

 

Saxreader reader = new saxreader ();

Document document = reader. Read (new file ("input. xml "));

Network Management Alliance www.bitscn.com

 

2. parse the text in XML format to obtain the document object. Bitscn_com

 

String text = "<members> </members> ";

Document document = incluenthelper. parsetext (text );

 

 

China Network Management Alliance www_bitscn_com

 

 

3. Create a Document Object.

 

Www.bitscn.com

 

 

Document document = incluenthelper. createdocument ();

Element root = Document. addelement ("members"); // create a root node

Network Management Network bitscn.com

 

Ii. node Problems

 

China Network Management Alliance WWW, bitscn, com

 

1. Get the root node of the document.

 

China Network Management Alliance www_bitscn_com

 

 

Element rootelm = Document. getrootelement ();

Network Management Alliance www.bitscn.com

 

2. Obtain a single subnode of a node.

 

Bitscn.com

 

 

Element memberelm = root. element ("member"); // "member" is the node name.

Bitscn_com

 

3. Get the node text: China Network Management Alliance WWW, bitscn, com

 

String text = memberelm. gettext (); can also be used:

String text = root. elementtext ("name"); this is the text that retrieves the name byte points under the root node.

 

 

Bitscn_com

 

4. retrieve all the byte points named "member" under a node and traverse www.bitscn.com

 

List nodes = rootelm. Elements ("member ");

 

For (iterator it = nodes. iterator (); it. hasnext ();){

Element elm = (element) it. Next ();

// Do something

}

Bitscn_com

 

5. traverse all subnodes under a node. Www.bitscn.com

 

For (iterator it = root. elementiterator (); it. hasnext ();){

Element element = (element) it. Next ();

// Do something

}

Www.bitscn.com

 

6. Add a subnode under a node.

 

Network Management Network bitscn.com

 

 

Element ageelm = newmemberelm. addelement ("Age ");

Network Management Network bitscn.com

 

7. Set the node text. Network Management Network bitscn.com

 

Ageelm. settext ("29 ");

 

 

Feedom.net

 

8. delete a node.

 

Www.bitscn.com

 

 

Parentelm. Remove (childelm); // childelm is the node to be deleted, and parentelm is the parent node.

 

3. Attribute-related.

 

Network Management Alliance www.bitscn.com

 

1. Obtain an attribute under a node

Element root = Document. getrootelement ();

Attribute attribute = root. Attribute ("size"); // attribute name

 

 

Www.bitscn.net

 

 

2. Get the attribute text

 

Network Management Network bitscn.com

 

String text = attribute. gettext (); can also be used:

String text2 = root. element ("name"). attributevalue ("firstname"); this is the value of the firstname attribute of the name byte point under the root node.

Network Management Network bitscn_com

 

3. traverse all attributes of a node

 

Bitscn.com

 

Element root = Document. getrootelement ();

For (iterator it = root. attributeiterator (); it. hasnext ();){

Attribute attribute = (attribute) it. Next ();

String text = attribute. gettext ();

System. Out. println (text );

}

Bitscn_com

 

4. Set the attributes and text of a node.

 

Www.bitscn.com

 

Newmemberelm. addattrielm ("name", "sitinspring ");

Bitscn.com

 

5. Set the attribute text

 

China Network Management Forum bbs.bitscn.com

 

Attribute attribute = root. Attribute ("name ");

Attribute. settext ("sitinspring ");

 

 

Www.bitscn.net

 

6. delete a property China Network Management Forum bbs.bitscn.com

 

Attribute attribute = root. Attribute ("size"); // attribute name

Root. Remove (attribute );

Www.bitscn.net

 

4. Write documents into XML files. China Network Management Forum bbs.bitscn.com

 

1. The document is in full English and is written directly without encoding. Network Management Alliance www.bitscn.com

 

Xmlwriter writer = new xmlwriter (New filewriter ("output. xml "));

Writer. Write (document );

Writer. Close ();

Www.bitscn.net

 

2. The document contains Chinese characters and sets the encoding format for writing.

 

Network Management Network bitscn_com

 

Outputformat format = outputformat. createprettyprint ();

Format. setencoding ("GBK"); // specify the XML Encoding

Xmlwriter writer = new xmlwriter (New filewriter ("output. xml"), format );

Writer. Write (document );

Writer. Close ();

5. Conversion of strings and XML

 

Www.bitscn.com

 

 

1. Convert the string to bitscn.com

 

String text = "<members> <member> sitinspring </member> </members> ";

Document document = incluenthelper. parsetext (text );

 

 

China Network Management Alliance WWW, bitscn, com

 

 

2. Convert the XML of the document or node into a string. China Network Management Alliance WWW, bitscn, com

 

Saxreader reader = new saxreader ();

Document document = reader. Read (new file ("input. xml "));

Element root = Document. getrootelement ();

String docxmltext = Document. asxml ();

String rootxmltext = root. asxml ();

Element memberelm = root. element ("member ");

String memberxmltext = memberelm. asxml ();

China Network Management Alliance WWW, bitscn, com

 

6. Use XPath to quickly find a node. China Network Management Forum bbs.bitscn.com

 

Example of the XML document to be read: bitscn_com

 

<? XML version = "1.0" encoding = "UTF-8"?>

<Projectdescription>

<Name> membermanagement </Name>

<Comment> </comment>

<Projects>

<Project> prj1 </Project>

<Project> prj2 </Project>

<Project> prj3 </Project>

<Project> prj4 </Project>

</Projects>

<Buildspec>

<Buildcommand>

<Name> org. Eclipse. jdt. Core. javabuilder </Name>

Network Management Alliance www.bitscn.com

 

<Arguments>

</Arguments>

</Buildcommand>

</Buildspec>

<Natures>

<Nature> org. Eclipse. jdt. Core. javanature </nature>

</Natures>

</Projectdescription>

 

 

Bitscn.com

 

Use XPath to quickly find the node project.

 

Feedom.net

 

Public static void main (string [] ARGs ){

Saxreader reader = new saxreader ();

Try {

Document Doc = reader. Read (new file ("sample. xml "));

List projects = Doc. selectnodes ("/projectdescription/projects/Project ");

Iterator it = projects. iterator ();

While (it. hasnext ()){

China Network Management Forum bbs.bitscn.com

 

 

Element elm = (element) it. Next ();

System. Out. println (ELM. gettext ());

}

}

Catch (exception ex ){

Ex. printstacktrace ();

}

}

 

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.