Common Operations for XML & dom_sax & dom4j

Source: Internet
Author: User

<? XML version = "1.0" encoding = "gb2312"?>

<! -- Student Information->
<? XML-stylesheet type = "text/CSS" href?#book.css ">
<Student>
<Person id = "001">
<Name> Zhang San </Name>
<Age> 28 </age>
<Address> Beijing </address>
<Person id = "001">
<Name> Li Si </Name>
<Age> 26 </age>
<Address> & lt; Shanghai & gt; </address>
<Htmlcode>
<! [CDATA [
Www.it315.org
]>
</Htmlcode>
</Person>
</Student>
This afternoon's course mainly centered on XML parsing. The most widely used XML parsing in Java applications is dom4j. The basic operations of dom4j during XML parsing include:
1. Get the root object: Element root = xmldoc. getrootelement ();
2. Get the root element name: String elementname = root. getname ();
3. Get the direct sub-element object: element = root. element ("element name") and list elementlist = root. Elements ();
4. Get the text in the Element Object: String text = element. gettext (); and string text = element. elementtext ("sub-element name ");
5. element attribute object: attribute = element. attribute ("attribute name"); and attribute = element. attribute (0); and list attributelist = root. attributes ();
6. Get attribute values: string value = attribute. getvalue (); and string value = element. attributevalue ("attribute name ");
7. Create document: Document document = incluenthelper. createdocument ();
8. Create element: element = Document. addelement ("element name ");
9. Create attributes: element. addattribute ("attribute name", "attribute value ");
10. Create text: element. addtext ("text ");
11. Delete nodes: Remove (element) and remove (attribute) and remove (Text text)
12. Call the asxml () method to convert the node into a string.
13. convert the string of the XML rule to xmldocument: string STR = "<student ID = '005 '> <Name> qianba </Name> <age> 26 </age> <mail> [email protected] </mail> </student> "document = incluenthelper. parsetext (STR );
14. Create an XML file:
Filewriter fw = new filewriter ("students2.xml ");
Xmlwriter writer = new xmlwriter (FW );
Writer. Write (document );
Writer. Close ();
15. Create a beautiful XML document and specify the character encoding:
Outputformat format = outputformat. createprettyprint ();
Format. setencoding ("GBK ");
Xmlwriter writer = new xmlwriter (FW, format );

Reproduced to: http://hi.baidu.com/maguowei/item/4a76d87bf48c333e714423a5

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.