This article describes the use of Jdom to read XML parsing in Java. Share it for your reference, as follows:
package com.yanek.demo.xml.test;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import org.j dom.Document;import org.jdom .Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;public class JdomReadXml { /** * @param args */ public static void main(S twist[] args) { /** * <?xml version="1.0" encoding="UTF-8"?> <actions m="001"><action * path="/test"><forward * name="success" url="test.jsp" /< forward name="failure" * url="failure.jsp" /></action><action path="/user" *><forward name="success" * url="test.jsp" /><forward name ="failure" url="failure.jsp" /></action></actions> */ SAXBuilder sax = new SAXBuilder(); Document doc; try { try { doc = sax.build(new File( "mystruts. xml")); Element root = doc.getRootElement(); List actions = root.getChildren(); // traverse to get the first-level child nodes under the root node and pass it in the recursive method as an incoming parameter for (Iterator i = actions .iterator(); i.hasNext();) { Element action = (Element) i.next(); System.out.println(action.getAttributeValue("path")); System.out.println(act ion.getAttributeValue ("class")); List forwards = action.getChildren(); for (Iterator j = forwards.iterator(); j.hasNext();) { Element forward = (Element) j.next(); System.out : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : .println(forward.getAttributeValue("name")); System.out.println(forward.getAttributeValue("url")); } } } catch (IOException e) { // TODO Au to-generated catch block e.printStackTrace( ); } } catch (JDOMException e) { e.printStackTrace(); } }}
Output:
/testcom.mystruts.demo.LoginActionsuccesstest.jspfailurefailure.jsp/usercom.mystruts.demo.UserActionsuccesstest.jspfailurefailure.jsp
I hope this article will be helpful to everyone's Java programming.