您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么實現java DOMParser 解析xml,代碼簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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 * ----------------------------------------------------------------------------- */ public class DOMExample { /* * +---------------------------------------------+ * | METHOD: main | * +---------------------------------------------+ */ 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()); } } /* * +---------------------------------------------+ * | METHOD: printElements | * +---------------------------------------------+ */ static void printElements(Document doc) { NodeList nodelist = doc.getElementsByTagName("*"); Node node; for (int i=0; i<nodelist.getLength(); i++) { node = nodelist.item(i); System.out.print(node.getNodeName() + " "); } System.out.println(); } /* * +---------------------------------------------+ * | METHOD: printElementAttributes | * +---------------------------------------------+ */ static void printElementAttributes(Document doc) { NodeList nodelist = doc.getElementsByTagName("*"); Node node; Element element; NamedNodeMap nnm = null; String attrname; String attrval; int i, len; len = nodelist.getLength(); for (int j=0; j < len; j++) { element = (Element)nodelist.item(j); System.out.println(element.getTagName() + ":"); nnm = element.getAttributes(); } if (nnm != null) { for (i=0; i<nnm.getLength(); i++) { node = nnm.item(i); attrname = node.getNodeName(); attrval = node.getNodeValue(); System.out.println(" " + attrname + " = " + attrval); } } System.out.println(); } /* * +---------------------------------------------+ * | METHOD: createURL | * +---------------------------------------------+ */ static URL createURL(String filename) { URL url = null; try { url = new URL(filename); } catch (MalformedURLException ex) { try { File f = new File(filename); url = f.toURL(); } catch (MalformedURLException e) { System.out.println("Cannot create URL for: " + filename); System.exit(0); } } return url; } }
上述內容就是怎么實現java DOMParser 解析xml,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。