sloppycode.net
XML parsing with JDOM
Using JDOM to parse an XML document (servlet example).
Home
›
Code snippets
›
Java
›
XML parsing with JDOM
This is an unfinished piece of code that was originally intended to search an XML document for a node. Whilst it's unfinished, it is illustrates how to use the JDOM XML package with a Servlet. JDOM can be found
here
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; import java.util.*; public class jdomtest extends HttpServlet { public Element findElement(Document xmldoc,String nodeName,HttpServletResponse response) throws ServletException, IOException { Element root = xmldoc.getRootElement(); // Traverse the tree List rootchildren = root.getChildren(); return recurse_findElement(rootchildren,nodeName,response); } private Element recurse_findElement(List rootchildren,String nodeName,HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); Element elcopy = null; // Cycle through all the child nodes of the root for (int i=0;i <= rootchildren.size() -1;i++) { Element el = (Element) rootchildren.get(i); // Display element name and value and attribute names/values of the node out.println("<U>"+el.getName()+":"+el.getText()+"</U><br>"); List elList = el.getAttributes(); for (int n=0;n <= elList.size() -1;n++) { Attribute atrib = (Attribute) elList.get(n); out.println(atrib.getName()+":"+atrib.getValue()+","); } String elname = el.getName(); if (elname.equals(nodeName)) { elcopy = el; } // If the node has children, call this method with the node if (el.hasChildren()) { List childnodes = el.getChildren(); recurse_findElement(childnodes,nodeName,response); } out.println("<BR><BR>"); } return elcopy; } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); try { // Build the document with SAX and Xerces, no validation SAXBuilder builder = new SAXBuilder(false); // Get the XML from a url String strUrl = request.getParameter("url"); Document doc = builder.build(strUrl); PrintWriter out = response.getWriter(); // Show the xml doc if the page is called with "showxml" if (request.getParameter("showxml") != null) { XMLOutputter fmt = new XMLOutputter(); fmt.output(doc, out); } else{ // Information about the XML document out.println("<html><body>"); out.println("File: "+strUrl+"<br>"); // Root info Element root = doc.getRootElement(); out.println("Root element:"+root.getName()+"<br>"); //If called with getnodeinfo, show all the nodes if (request.getParameter("getnodeinfo") != null) { Element f = findElement(doc,"mkdir",response); } } } catch (JDOMException e) { PrintWriter out = response.getWriter(); out.println(e.getMessage()); } } }
{Name}
Says:
{Date}
{Text}
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search
› Sitemap
Buy on Amazon
Buy on Amazon
Buy on Amazon
Buy on Amazon