Results 1 to 5 of 5
Thread: XML parsing using DOM
- 10-08-2009, 10:44 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
XML parsing using DOM
Hello,
I'm need to extract the contents of, say, the <Date><\Date> tags.
I'm using this sample code, but when the program runs nothing happens, there's no actual output.
Can you please suggest what could be wrong?Java Code:import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; public class XMLParse { public void test() { try { DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = docbuilder.parse(new File("C:\\Java\\TestFile.xml")); Element qu = doc.getElementById("Date"); doc.getDocumentElement().normalize(); System.out.println(qu.getTextContent()); } catch(Exception e) {} } }
Thanks for your help!
-
ouch!Java Code:catch(Exception e) {}
You sure you want to do this?
- 10-09-2009, 10:06 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
Hi Fubarable,
Yes I know about that, I would elaborate further on the code in regards to exceptions, this is just a test program to check about parsing.
Thanks.
- 10-14-2009, 05:59 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
The reason Fubarable mentioned your Exception is that there is a possibility that an exception is being caught and you are not seeing anything because of it. Basic try catch procedure suggests you at least have some type of console output inside the structure of catch(Exception e){}
Remove:
ADD:Java Code:catch(Exception e){}
This will better aid you in future problems and could have helped you avoid this situation altogether. There may be a null pointer exception.Java Code:catch(Exception e){e.printStackTrace();}
Now, in regards to your problem.
Best practice it to use xpath when you are extracting elements from an XML. Inherent nature of a DOM tree is unintuitive and can give rise to strange problems where you grab empty text nodes if your XML has strange formatting.
If you do not like xpath:
Then you are in for a wold of pain imho :D try qu.getNodeValue() or qu.getNodeName() to make sure that you are actually getting the <Date> element. I will run your code and see what I get.Last edited by BeMathis; 10-14-2009 at 06:04 PM.
- 10-14-2009, 06:17 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
try this. its a little dirty but it may be a step in the right direction
Java Code:try{ DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = docbuilder.parse(new File("C:\\Java\\TestFile.xml")); NodeList qu = (NodeList) root.getElementsByTagName("standard_data_type"); for(int x=0; x<qu.getLength(); x++){ System.out.println(qu.item(x).getNodeName()); qu.item(x).normalize(); System.out.println(qu.item(x).getTextContent()); } }catch(Exception e) {e.printStackTrace();}Last edited by BeMathis; 10-14-2009 at 06:19 PM.
Similar Threads
-
Parsing XML
By virvalid in forum Advanced JavaReplies: 3Last Post: 08-10-2009, 12:40 PM -
Parsing
By Matt Sakko in forum New To JavaReplies: 14Last Post: 03-21-2009, 04:49 PM -
Xml Parsing
By Nomad in forum XMLReplies: 12Last Post: 02-22-2009, 11:19 AM -
xml parsing help
By tankhardrive in forum XMLReplies: 2Last Post: 02-11-2009, 11:45 PM -
Parsing URL
By Java Tip in forum Java TipReplies: 0Last Post: 12-26-2007, 10:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks