<? 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