tags:

views:

3809

answers:

9

Hi

How can I check whether a node exists in an XML file, and also count the number of nodes.

I have one XML file for an example:

 <Employee>
  <Emp>
    <Name id="1">   A     </Name>
    <Name id="2">   C     </Name>
    <Name id="3">   D     </Name>
   </Emp>
  </Employee>

Thanks & Regards

Ravikumar

A: 

There are at least 4 nodes here, assuming that your </Emp> is matched by an opening <Emp> tag: <Emp>, <Name>, ID, and the string " D " would all be represented as nodes. It's not clear from your question whether you really would want to count all of these. I'm also not sure whether you want to determine the existence of a specific one of them.

Ultimately, though, XPath is probably what you're looking for.

Dan Breslau
A: 

As an alternative to XPath, many languages that have XML DOM support will allow you to call a method on an XML Document like:

GetAllNodesWithTagName(string tagname);

Your code to see if it exists would look something like this (written in pseudocode):

int num_nodes = 0;
string node_name = "Name"; // want to find all of the <Name> tags
XMLNode [] nodes = GetNodesWithTagName(node_name);
num_nodes = nodes.Length;

XPath is good, but it's better suited for easily navigating an XML document in interesting and complex ways. This code will be a bit more straightforward than the corresponding XPath code.

Aaron
+2  A: 

With linq 2 xml in c#:

var employee = XElement.Load(someStream);
var emp = employee.Element("Emp");
if( emp != null )
{
   int count = emp.Elements("Name").Count();
}
eglasius
A: 

getElementsByTagName["tagname"] is also a DOM method which can be used to get a node.If the node does not match, method with return null.

AvidProgrammer
A: 

If you are programming in Java there are two related libraries you should look at.

JDOM - http://www.jdom.org/ DOM4J - http://www.dom4j.org/

I'd look at Dom4j 2.0 now since it's got support for generics, XPath, and now has some better high level support. Dom4j I think was forked from the earlier jdom.

In either you can read XML from a file, URL, string etc, parse it and check for nodes in only a few lines of code.

jottos
+1  A: 

I'm assuming you're using XSL to transform this document then I would assume that a variable would give the best functionality. You'd use this:

<xsl:variable name="Name_Count" select="count(//Name)"/>

This will give you the number of nodes of 'Name' and you can change that to anything you'd like. Obviously if it's zero then there are none, otherwise it's the count.

Good luck.

Stephen Friederichs
A: 

LINQ is great. But just in case you are stuck on a system with .NET 2.x you might have to do it the "old" (XPath) way (where xmlFragment is your string of XML above):

XPathDocument doc = new XPathDocument(new StringReader(xmlFragment));
XPathNavigator n = doc.CreateNavigator().SelectSingleNode("//Name[@id='4']");
if(n==null){//Node does not exist}
rasx
A: 

int nNodeExistCount = xmlOuput.GetElementsByTagName("NodeName").Count;

if (nNodeExistCount>0) { Response.write(" The NodeName exists!"); } else { Response.write(" The NodeName does not exist!"); }

Cheers, Mani

A: 

Hi

If you are using XSLT transformation just trythis:

< xsl:choose>

< xsl:when test="//Employee/Emp">

< -- Node exists-->

< /xsl:when>

< xsl:otherwise>

< --Node does not exist-->

< /xsl:otherwise>

< /xsl:choose>

Pramodh