| Author |
ParserImpl for DOM
|
Bhasker Reddy
Ranch Hand
Joined: Jun 13, 2000
Posts: 176
|
|
iS there code examples for implementing DOM parser. I need implementation for these methods to extract values from XML files using DOM parser // Methods public Node parse(File fileName) { return null;} public Node[] getChildren(Node node) { return null;} public Node[] getChildByName(Node node, String name) { return null;} public Node getFirstChildByName(Node node, String name) { return null;} public String[] getNodeValueByName(Node node, String name) { return null;} public String getFirstNodeValueByName(Node node, String name) { return null;} public Attr[] getAttributes(Node node) { return null;} public String getAttributesByName(Node node, String name) { return null;} public Node[] getNodeDrilled(Node node, String[] path) { return null;} public Node getFirstNodeDrilled(Node _node, String[] _path) { return null;} public void fillObject(Node node, String[] path, Object objToFill) { }
|
Bhasker Reddy
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
Are you really implementing your own DOM parser? Why can't you use an existing, time-tested implementation such as Apache Xerces or the one that comes with J2SE 1.4?
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
Bhasker Reddy
Ranch Hand
Joined: Jun 13, 2000
Posts: 176
|
|
I am using DOM parser. But I am trying to write some methods to extract values. package nortel.xml.datatypes; import java.io.File; import org.w3c.dom.Attr; import org.w3c.dom.Node; import org.w3c.dom.*; import java.util.*; import org.xml.sax.*; //import org.jdom.input.*; import org.apache.xerces.parsers.DOMParser; import java.io.*; import java.util.logging.FileHandler; import javax.xml.parsers.*; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */ public class ParserImp { public ParserImp() { } // Methods public Node parse(File fileName) { try{ /* Document doc = null ; InputStream ins = new FileInputStream( f ); InputStreamReader isr = new InputStreamReader( ins, "UTF-8" ); LineNumberReader lnr = new LineNumberReader( isr ); InputSource isrc = new InputSource( lnr ); try { DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setValidating( false ); // default is false fac.setIgnoringComments( true ); fac.setIgnoringElementContentWhitespace( true ); DocumentBuilder builder = fac.newDocumentBuilder(); // doc = builder.parse( f ); doc = builder.parse( isrc ); */ InputStream ins = new FileInputStream( fileName ); InputStreamReader isr = new InputStreamReader( ins, "UTF-8" ); LineNumberReader lnr = new LineNumberReader( isr ); InputSource isrc = new InputSource( lnr ); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setValidating(false); fac.setIgnoringComments(true); fac.setIgnoringElementContentWhitespace( true ); DocumentBuilder builder = fac.newDocumentBuilder(); Document doc = builder.parse(isrc); Node node = doc.getFirstChild(); // Node node = null; // Node node = doc.getParentNode(); System.out.println("node type" + node.getNodeType()); System.exit(-1); System.out.println("$$$$$$$$$$$$$$$NODE.NODENAME:::::::" + fileName); return node; //* To read from string /* StringReader stringreader = new StringReader(fileName.toString()); InputSource stringInputSource = new InputSource(stringreader); DOMParser parser = new DOMParser(); parser.parse(stringInputSource); Document document = parser.getDocument(); Node node = document.getParentNode(); return node;*/ } catch (Exception e) { System.err.println("XML Parser Exception: " + e.getMessage()); e.printStackTrace(System.err); } return null; } public static Node[] getChildren(Node node) { System.out.println("Node:getNodeNAme::::" + node.getNodeName()); NodeList nodeList = node.getParentNode().getChildNodes(); Node[] nodeArray = new Node[nodeList.getLength()]; for (int idx=0; idx<nodeList.getLength(); idx++){ nodeArray[idx] = nodeList.item(idx); } return nodeArray; } public static Node[] getChildByName(Node node, String name) { Node[] nodeArray = getChildren(node); ArrayList arrList = new ArrayList(); for (int idx=0; idx<nodeArray.length; idx++){ if (nodeArray[idx].getParentNode().getNodeName().equals(name)){ arrList.add(nodeArray[idx]); } } Node[] nodeArrayRet = new Node[arrList.size()]; ListIterator itr = arrList.listIterator(); int idx=0; while (itr.hasNext()){ nodeArrayRet[idx] = (Node)itr.next(); idx++; } return nodeArrayRet; } public static Node getFirstChildByName(Node node, String name) { Node[] nodeArray = getChildren(node); for (int idx=0; idx<nodeArray.length; idx++){ if (nodeArray[idx].getParentNode().getNodeName().equals(name)) return nodeArray[idx]; } return null; } public static String[] getNodeValueByName(Node node, String name) { return null;} public static String getFirstNodeValueByName(Node node, String name) { Node[] nodeArray = getChildren(node); for (int idx=0; idx<nodeArray.length; idx++){ if (nodeArray[idx].getParentNode().getNodeName().equals(name)) return nodeArray[idx].getParentNode().getNodeValue(); } return null; } public Attr[] getAttributes(Node node) { return null;} public String getAttributesByName(Node node, String name) { return null;} public Node[] getNodeDrilled(Node node, String[] path) { return null;} public Node getFirstNodeDrilled(Node _node, String[] _path) { return null;} public void fillObject(Node node, String[] path, Object objToFill) { } } It's crazy. I cannot understand it. I tried to extract node values from xml file. First by using parse method, get the Main node(Document node) and then pass that node to a datatype class, that inturn calls the methods in these classes to get values. Can you guys help me out.
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12267
|
|
|
Read the documentation in the org.w3c.dom.Node JavaDocs. Many types of node have null for the node value and this frequently trips people up.
|
 |
 |
|
|
subject: ParserImpl for DOM
|
|
|