1. Loading an XML document Saxreader reader = new Saxreader (); Document doc = Reader.read ("src/book.xml"); 2. Get the root element Node root = Doc.getrootelement (); or Element r = doc.getrootelement (); 3. Get a single child node of a node Element root = Root.getrootelement (); Element memberelm=root.element ("member"); //"member" is a node name 4. Get the text of the node String Text=memberelm.gettext (); < Span style= "margin:0px; padding:0px ">5. Get a node named" book "all the byte points and traverse element r = doc.getrootelement (); list titles = r.elements ("book ") ; for (int i=0;i<titles.size (); i++) { &NBSP; System.out.println (( Element) Titles.get (i)). Asxml ()) ; " 6. Adds a child node under a node. element Ageelm = newmemberelm.addelement ("Age"); 7. Set the node text. ("Ageelm.settext"); 8. Delete a node. Parentelm.remove (Childelm); //Childelm is the node to be deleted, Parentelm is its parent node < Span style= "margin:0px; padding:0px "> property related. 1. Obtain a property under a node element root=document.getrootelement (); &NBSP attribute attribute=root.attribute ("size ");// property name name 2. Gets the text of the attribute string text=attribute.gettext (); also available with: string text2=root.element ("name "). AttributeValue (" firstname "); This is the value of the attribute firstname that gets the name byte point under the root node. 3. Traverse all properties of a node
Element root = Document.getrootelement (); for (Iterator it = Root.attributeiterator (); It.hasnext ();) {Attribute Attribute = (Attribute) it.next (); String text = Attribute.gettext (); System.out.println (text);} 4. Set the properties and text of a node. Newmemberelm.addattribute ("name", "sitinspring"); 5. Set the text of a property Attribute attribute=root.attribute ("name"); attribute.settext ("sitinspring"); 6. Delete a property Attribute Attribute = Root.attribute ("size"); //attribute name NamE Root.remove (attribute);Writes the document to an XML file. 1. The document is all in English, does not set the encoding, the direct writing form.
XMLWriter writer = new XMLWriter (New FileWriter ("Output.xml")); Writer.write (document); Writer.close (); 2. The document contains Chinese, formatting the format of the written form of the encoding.
OutputFormat format = Outputformat.createprettyprint (); Format.setencoding ("GBK"); Specifies the XML encoding XMLWriter writer = new XMLWriter (New FileWriter ("Output.xml"), format); Writer.write (document); Writer.close ( ); Conversion of character strings to XML 1. Converting a string to XML
String Text = "<members> <member>sitinspring</member> </members>";D ocument Document = Documenthelper.parsetext (text); 2. Convert the XML of a document or node to a string.Saxreader reader = new Saxreader ();D ocument 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 (); quickly find nodes using XPath. example of a read XML document <?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><arguments> </arguments></buildCommand></buildSpec><natures><nature> Org.eclipse.jdt.core.javanature</nature></natures></projectdescription>
Use XPath to quickly find node project.
public static void Main (string[] args) {Saxreader reader = new Saxreader (); try {Document doc = reader.read (new File ("Sampl E.xml ")); List projects = Doc.selectnodes ("/projectdescription/projects/project"); Iterator it = Projects.iterator (); while ( It.hasnext ()) {element elm = (Element) It.next (); System.out.println (Elm.gettext ());}} catch (Exception ex) {ex.printstacktrace ();}}
|