Xml parsing-dom parsing of jaxp

Source: Internet
Author: User

Package day06_parser.dom;/*** xml parsing technology: JAXP is the abbreviation of Java API for XML Processing. * The Chinese meaning is: programming Interface for XML document processing written in Java. JAXP supports DOM, SAX, XSLT, and other standards. * Below we study two ways of parsing: * 1. dom parsing 2. sax parsing: Simple API for XML * Below is an example of dom parsing. * JAXP-DOM parsing instance: * The following instance implements the function through javax. xml package implements dom-based xml parsing * specific operations include adding nodes, deleting nodes, modifying node content, and querying node information */import java. io. file; import javax. xml. parsers. *; import javax. xml. transform. *; import javax. xml. transform. dom. *; import javax. xml. transform. stream. streamResult; import org. junit. test; import org. w3c. dom. *; public class DOMCURD {// you can use the junit test tool to test public static void main (String [] args) throws Exception {demo05 ();} // obtain the Document object associated with the parser @ Testpublic static void demo01 () throws Exception {// ==================== get document ==================================== ========/// 1. obtain the factory DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // 2. obtain the parser DocumentBuilder builder = factory. newDocumentBuilder (); // 3. obtain the Document Object @ SuppressWarnings ("unused") Document document = builder Based on the parser. parse (new File ("db. xml ");} // query public static void demo02 () throws Exception {// 1. obtain documentDocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse (new File ("db. xml "); // 2. get the root Element -- books (can be omitted) @ SuppressWarnings ("unused") Element rootElement = document. getDocumentElement (); // 3. obtain all the book elements. The attribute idNodeList allBookElements = document. getElementsByTagName ("book"); // 4 traverse the book element ---- db of this instance. xml contains two book elements for (int I = 0; I

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.