Java核心代码例程之:(JAXP) DOM

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

import Javax.XML.parsers.*;

import org.w3c.dom.*;

import org.xml.sax.*;

/**

* DOMDemo uses JAXP to acquire a DocumentBuilder to build a DOM Document from an XML file.

* The example XML file represents a shopping cart.

*

* The following JARs must be in your CLASSPATH:

* - jaxp.jar

* - xerces.jar (for SAX parser and DOM object implementations)

*

* Download JAXP (which includes these JARs) here: http://java.sun.com/xml/

* Find additional Xerces info here: http://xml.apache.org/

*

**/

public class DOMDemo

{

public static void main( String[] args )

{

try

{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

System.out.println( "DocumentBuilderFactory classname: " + factory.getClass().getName() );

DocumentBuilder builder = factory.newDocumentBuilder();

System.out.println( "DocumentBuilder classname: " + builder.getClass().getName() );

//parse the XML file and create the Document

Document document = builder.parse( "cart.xml" );

/*

At this point, all data in the XML file has been parsed and loaded into memory

in the form of a DOM Document object. The Document is a tree of Node objects.

This printNode() method simply recurses through a Node tree and displays info

about each node.**/

printNode( document, "" );

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航