Java XML API Simple use example
Main steps:
1. Create Documentbuilderfactory objects
2, read the document, get Document object documents
3, parsing XML elements, such as Doc.getelementsbytagname ()
Java JDK XML parsing is the same as the standard, so the more JS XML DOM method name is almost
Package com.test;
Import java.io.IOException;
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.NodeList;
Import org.xml.sax.SAXException;
public class Xmltest {public static void main (string[] args) {new Xmltest (). Xmlinput ("D:/img/orgcore.xml"); public void Xmlinput (String filepath) {//Create Documentbuilderfactory object documentbuilderfactory DBFC = Documentbuilde
Rfactory.newinstance ();
try {documentbuilder builder = Dbfc.newdocumentbuilder ();
Read Document DOC = Builder.parse (filepath);
The following are some methods for parsing XML documents SYSTEM.OUT.PRINTLN ("Return to Node");
Element d = doc.getdocumentelement ();
String s = d.getnodename ();
System.out.println (s);
System.out.println (D.getnodevalue ());
System.out.println (D.getnodetype ());
System.out.println ("----------------"); System.out.println (Doc.getnodeName ());
System.out.println (Doc.getnodevalue ());
System.out.println (Doc.getnodetype ());
System.out.println ("----------------");
System.out.println (Doc.getxmlencoding ());
System.out.println (Doc.getxmlversion ());
System.out.println (Doc.getxmlstandalone ());
System.out.println ("----------------");
Gets the XML element nodelist nlist = doc.getelementsbytagname ("fixed"); for (int i = 0; i < nlist.getlength (); i++) {//Parse element attribute System.out.println (Nlist.item (i) getattributes (). GetName
Ditem ("facet"));
} System.out.println ("----------------");
catch (Parserconfigurationexception e) {e.printstacktrace ();
catch (Saxexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}
}
}