Java XML API Simple use example

Source: Internet
Author: User

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 ();
 }
	}
}


Related Article

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.