Code snippets, code sharing, PHP code sharing, Java code sharing, Ruby code sharing, Python code sharing, HTML code sharing, CSS code sharing, SQL code sharing, and JavaScript code sharing
Import java. io. *; import java.net. *; import org. w3c. dom. *; import org. w3c. dom. node. *; import oracle. xml. parser. v2. *;/*** ----------------------------------------------------------------------------------- * Demonstrate how to use DOM. ** @ version 1.0 * @ author Jeffrey M. hunter (jhunter@idevelopment.info) * @ author http://www.idevelopment.info * Optional */public class DOMExample {/** + --------------------------------------------- + * | METHOD: main | * + signature + */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 varous 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 () ;}/ ** + ------------------------------------------------- + * | METHOD: printElements | * + principal + */static void printElements (Document doc) {NodeList nodelist = doc. getElementsByTagName ("*"); Node node; for (int I = 0; I
The above is the detailed description of the java DOMParser xml parsing instance. For more information, see other related articles in the first PHP community!