Results 1 to 10 of 10
- 08-09-2012, 06:25 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Check whether the XML Tag exists while parsing the XML using DOM in Java
Hi,
I have an XML with several nested nodes where the XML Tags are generated dynamically. I have developed a generic program where I am able to parse all XML elements successfully using DOM. But, as the nodes and elements in XML are optional (I wont get the Tag if no data is mapped to it), I need to check for the existence of each tag. Could anyone suggest me, which method I can use to find out the existence of the node while parsing the XML.
- 08-09-2012, 06:33 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
Don`t understand...if there is no tag, you'll not get one of these elements while iterating through the DOM?

Or how do you parse the xml and how do you select the tags? By tag name? oO (not sure, but the element would be null then, so you could check this?!)
- 08-10-2012, 01:05 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
Actually, I need to write a fixed field length flat file with the parsed XML data. I am Parsing the XML using getTagValue("TagName") method. If the specified tag is not available in the given input XML, it is throwing a NullPointerException. If there is no tag available in XML, I have to write a fixed space to the flat file for that specific tag data. So, how can I handle that. Need help.
Last edited by SindhuP; 08-10-2012 at 01:07 PM.
- 08-10-2012, 01:37 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
What class is getTagValue() part of?
What is the full stack trace for the NPException?
Where in your code is that being thrown?Please do not ask for code as refusal often offends.
- 08-10-2012, 02:37 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
I am getting the Null pointer Exception while calling the getTagValue() method for "B4K02" as there is no tag available with the name in XML.
Please find the code below.
The XML is:Java Code:import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.OutputStream; import java.io.OutputStreamWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXML { public static void main(String argv[]) { try { File fXmlFile = new File("D:\\file.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); FileWriter fstream = new FileWriter("D:\\XMLFlat.txt"); BufferedWriter out = new BufferedWriter(fstream); NodeList nList = doc.getElementsByTagName("B4K"); System.out.println("-----------------------"); StringBuffer str = new StringBuffer(); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; str.append("B4K"); String b4k01 = getTagValue("B4K01", eElement); String b4k01field = addSpace(b4k01,Consts.B4K01); str.append(b4k01field); String b4k02 = getTagValue("B4K02", eElement); String b4k02field = addSpace(b4k02,Consts.B4K02); str.append(b4k02field); String b4k03 = getTagValue("B4K03", eElement); String b4k03field = addSpace(b4k03,Consts.B4K03); str.append(b4k03field); } out.write(str.toString()); out.close(); } } catch (Exception e) { e.printStackTrace(); } } private static String getTagValue(String sTag, Element eElement) { NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = (Node) nlList.item(0); return nValue.getNodeValue(); } }
Java Code:<?xml version="1.0"?> <B4K> <B4K01>04</B4K01> <B4K03>Sample</B4K03> </B4K>
Last edited by Tolls; 08-10-2012 at 02:54 PM. Reason: Add code tags
- 08-10-2012, 02:56 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
Yes, but where in getTagValue is it throwing the exception.
That's your method after all...
Also, please use [code] tags [/code] in future to retian formatting.Please do not ask for code as refusal often offends.
- 08-10-2012, 03:04 PM #7
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
When you get the node, you're not checking it's valid before you do a get value. If the tag doesn't exist, there's no object to actually getValue from. You need to make sure the node list is valid and then if it is, get a node from item 0, make sure it's valid, and then get it's value.
- 08-10-2012, 03:07 PM #8
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
It is throwing a NullPointerException exception at,
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChi ldNodes();
- 08-10-2012, 03:23 PM #9
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: Check whether the XML Tag exists while parsing the XML using DOM in Java
So, for every tag I have to check with the node names which I get when I iterate through the root child nodes...rite??
- 08-10-2012, 03:48 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Check if file exists before download
By dman in forum NetworkingReplies: 0Last Post: 04-28-2011, 03:57 PM -
How do I check if a database exists ...Any help?
By nmvictor in forum New To JavaReplies: 5Last Post: 05-09-2010, 04:21 PM -
How to check whether file is exists or not
By Java Tip in forum java.ioReplies: 0Last Post: 04-05-2008, 10:13 AM -
Check if a web page exists or not
By Java Tip in forum Java TipReplies: 0Last Post: 03-02-2008, 07:24 PM -
How an array can check that profile whether exists or not???
By hien_NU in forum New To JavaReplies: 1Last Post: 01-10-2008, 01:18 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks