import java.io.*;import java.net.*;import org.w3c.dom.*;import org.w3c.dom.Node.*; import oracle.xml.parser.v2.*; public class DOMExample { static public void main(String[] argv) { try { if (argv.length != 1) { // must pass in the name of the XML file System.err.println("Usage: java DOMExample filename"); System.exit(1); } // Get an instance of the parser DOMParser parser = new DOMParser(); // Generate a URL from the filename URL url = createURL(argv[0]); // Set various parser options; validation on, // warnings shown, error stream set to stderr. parser.setErrorStream(System.err); parser.setValidationMode(true); parser.showWarnings(true); // parse the document parser.parse(url); // Obtain the document XMLdocument doc = parser.getdocument(); // print document elements System.out.print("The elements are: "); printElements(doc); // print document elements attributes System.out.println("The attributes of each element are: "); printElementAttributes(doc); } catch (Exception e) { System.out.println(e.toString()); } } static void printElements(document doc) { NodeList nodelist = doc.getElementsByTagName("*"); Node node; for (int i=0; i以上就是java DOMParser 解析xml的实例详解的详细内容,更多请关注考高分网其它相关文章!



