parsing XML files in the Java JDK using the Org.w3c.dom.Document tool
Write the whole process:
Import Java.io.file;import Java.io.ioexception;import Java.io.stringwriter;import Javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.parsers.parserconfigurationexception;import Javax.xml.transform.transformer;import Javax.xml.transform.transformerconfigurationexception;import Javax.xml.transform.transformerfactory;import Javax.xml.transform.dom.domsource;import Javax.xml.transform.stream.streamresult;import org.w3c.dom.Document; Import Org.w3c.dom.element;import Org.xml.sax.saxexception;public class Creatxml {/** * Create an XML file * @param args */public s tatic void Main (string[] args) {//TODO auto-generated method stubtry {documentbuilderfactory factory= Documentbuilderfactory.newinstance ();//can get XML parser Documentbuilder builder = Factory.newdocumentbuilder ();// Gets the document generator documents Document=builder.newdocument ();//Gets the document instance element Root=document.createelement ("languages");// Create a root element Root.setattribute ("Cat", "it");//Add a cat= "it" attribute element to the root element lan1=document.creAteelement ("LAN");//Create an element tag named <lan>lan1.setattribute ("id", "1");//Add attribute element name1=document.createelement ("name");//Create a name tag name1.settextcontent ("Java");//Set the content for the name tag element ide1=document.createelement ("IDE");// Create an IDE tag ide1.settextcontent ("Eclipse");//Add Content Lan1.appendchild (NAME1) to the table, Lan1.appendchild (IDE1);//Let name1 and The ide1 tag becomes a child node of the lan1 tag root.appendchild (lan1);//Add Lan1 to the child node of root document.appendchild (root);//Add root to Document// Convert document to stream data for storage in file or transmit Transformerfactory transformerfactory= transformerfactory.newinstance (); Transformer Transformer=transformerfactory.newtransformer (); StringWriter writer=new StringWriter () transformer.transform (new Domsource (document), new Streamresult (writer));// Convert document to output stream System.out.println (writer.tostring ()); Transformer.transform (new Domsource (document), new Streamresult ("Newxml.xml"));//Save document to file} catch (Exception e) {e.printstacktrace ();}}}
The following is the full resolution:
Import Java.io.file;import Javax.xml.parsers.documentbuilder;import javax.xml.parsers.DocumentBuilderFactory; Import Javax.xml.parsers.parserconfigurationexception;import Org.w3c.dom.document;import org.w3c.dom.Element; Import Org.w3c.dom.node;import org.w3c.dom.nodelist;/** * Parse XML file * * */public class Showxml {public static void main (STR Ing[] args) throws Exception {documentbuilderfactory factory=documentbuilderfactory.newinstance ();// The XML parser can be obtained Documentbuilder builder=factory.newdocumentbuilder ();//Gets the document generator documents Document=builder.parse (The new File (" Workspace.xml "));//Get document Element Root=document.getdocumentelement ();//Get document elements (this is the main element) System.out.println (" version= " +root.getattribute ("version"));//Output view element properties NodeList List=root.getelementsbytagname ("component");// Get all component label nodes for (int i = 0; i < list.getlength (); i++) {//Traverse node Element component= (Element) List.item (i);//Get Node Syst Em.out.println ("----------------------------------"); SYSTEM.OUT.PRINTLN ("Component" +i+ ". Name=" +component.getattribute ("Name "));//output node element attribute//system.out.println (Component.gettextcontent ());//Gets the text within the node nodelist list1= Component.getchildnodes ();//Gets all child elements of the component element for (int j = 0; J < List1.getlength (); Traverse the child element System.out.println ("-----------------------------------------"); Node Componentchild=list1.item (j);//Get child element node System.out.println (Componentchild.getnodename ());//output sub-element tag name// System.out.println (Componentchild.gettextcontent ());//output sub-element text content//output Some useless node #text represents the line of blank nodes//Remove #text method Determine if this node is an element and then output/*if (componentchild instanceof Element) {System.out.println (Componentchild.getnodename ());// Output child element tag name}*/}}}}
There is also a reference Dom4j.jar toolkit
Write XML
Package Dom4j_xml;import Java.io.bufferedoutputstream;import Java.io.file;import java.io.fileoutputstream;import Org.dom4j.document;import Org.dom4j.documenthelper;public class Dom4j {/** * dom4j use fast XML read/write * @param args * @throws Ex Ception */public static void Main (string[] args) throws Exception {//TODO auto-generated method stubstring xmlval= "&L t;peopre><name> cousin </name><age>26</age></peopre> ";D ocument document= Documenthelper.parsetext (Xmlval); System.out.println (Document.asxml ());//Direct output of XML format data file File=new file ("Mynews.xml"); if (!file.isabsolute ()) { File.createnewfile ();} Byte[] Xmlbyte=xmlval.getbytes ("Utf-8"); FileOutputStream out=new fileoutputstream (file); Bufferedoutputstream bout=new Bufferedoutputstream (out); Bout.write (xmlbyte); Bout.flush (); Out.close (); Bout.close ( );}}
XML file parsing!!!