package net.programmera.www.dom; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Text; public class MyDOMParser { /** * parse * * This method will parse a XML-document and return a Document. * * Arguments: * url= The URL where to find the XML-document to be parsed. */ public Document parse(String url){ try{ DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); DocumentBuilder db= dbf.newDocumentBuilder(); Document doc= db.parse(url); return doc; }catch(Exception e){ System.out.print(e); return null; } } /** * getStructure * * This method will return the structure of a Document. * * Arguments: * doc= a Document. */ public String getStructure(Document doc){ Node n= doc.getDocumentElement(); String outStr= getElement(n,0); return outStr; } /** * getElement * * Helper method. * */ private String getElement(Node n, int level){ String baseStr=""; // 0. Indent the text for(int i=0; i< level; i++){ baseStr+="__"; } // 1. Output the element name String outStr=baseStr+"ELEMENT="+n.getNodeName(); // 2. Output attributes if(n.hasAttributes()){ NamedNodeMap nnm= n.getAttributes(); for(int i=0; i< nnm.getLength(); i++){ Attr a = (Attr) nnm.item(i); outStr+="#"+baseStr+"__ATTR="+a.getName()+" VAL="+a.getValue()+"="; } } // 3. Output the children if(n.hasChildNodes()){ NodeList nl = n.getChildNodes(); for(int i=0; i < nl.getLength(); i++ ){ Node childNode = nl.item(i); if(childNode.getNodeType()== Node.ELEMENT_NODE){ outStr+= "#"+ getElement(childNode, level+1); }else if(childNode.getNodeType()== Node.TEXT_NODE){ Text tn = (Text) childNode; outStr+= "#"+ baseStr + "__TEXT=" + tn.getData()+"="; } } } return outStr; } }