Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By ics1010

Thread: Check whether the XML Tag exists while parsing the XML using DOM in Java

  1. #1
    SindhuP is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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?!)

  3. #3
    SindhuP is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default 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.

    ** This space for rent **

  5. #5
    SindhuP is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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.

    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();
      }
      
    }
    The XML is:
    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

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default 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.

    ** This space for rent **

  7. #7
    ics1010 is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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.
    Tolls likes this.

  8. #8
    SindhuP is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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();

  9. #9
    SindhuP is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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??

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: Check whether the XML Tag exists while parsing the XML using DOM in Java

    Well yes, because this:
    eElement.getElementsByTagName(sTag)
    will be returning an empty list, so this:
    eElement.getElementsByTagName(sTag).item(0)
    will return null.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Check if file exists before download
    By dman in forum Networking
    Replies: 0
    Last Post: 04-28-2011, 03:57 PM
  2. How do I check if a database exists ...Any help?
    By nmvictor in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 04:21 PM
  3. How to check whether file is exists or not
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  4. Check if a web page exists or not
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:24 PM
  5. Replies: 1
    Last Post: 01-10-2008, 01:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •