如何检查XML文件中是否存在节点?

时间:2021-10-18 23:50:09

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

如何检查XML文件中是否存在节点,并计算节点的数量?

I have one XML file for an example:

我有一个XML文件作为例子:

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

10 个解决方案

#1


6  

With linq 2 xml in c#:

使用c#中的linq 2 xml:

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

#2


2  

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来转换此文档,那么我假设一个变量将提供最佳的功能。你会用这个:

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

这将给出名称节点的数量,您可以将其更改为任何您想要的。显然,如果它是零,那就没有,否则就是计数。

#3


2  

XmlDocument _xmlDoc = new XmlDocument();

_xmlDoc.Load(Server.MapPath("~/XMLFile.xml"));

XmlNode _node = _xmlDoc.SelectSingleNode("Employee/Emp");

if (_node != null)

{

    XmlNodeList _nodeList = _node.SelectNodes("Name");

    Response.Write(_nodeList.Count);
}

else

{

    Response.Write("Emp node doesnot exist");

}

#4


1  

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

如果您正在用Java编程,那么您应该查看两个相关的库。

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

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.

我现在将介绍Dom4j 2.0,因为它支持泛型、XPath,现在有了一些更好的高级支持。我认为Dom4j是从早期的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.

您可以从文件、URL、字符串等中读取XML,解析它,并仅在几行代码中检查节点。

#5


1  

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):

LINQ是伟大的。但万一你被困在。net 2的系统中。您可能需要使用“old”(XPath)方法(xmlFragment是您上面的XML字符串):

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

#6


0  

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.

这里至少有4个节点,假设您的与一个开放的 标记匹配: , ID,以及字符串" D "都将表示为节点。你的问题不清楚你是否真的想把所有这些都算上。我也不确定你是否想确定其中一个的存在。

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

但是,最终,XPath可能是您要寻找的。

#7


0  

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

作为XPath的替代品,许多具有XML DOM支持的语言将允许您在XML文档上调用方法,比如:

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.

XPath很好,但它更适合以有趣而复杂的方式轻松导航XML文档。这段代码将比相应的XPath代码简单一些。

#8


0  

If you are using XSLT transformation just trythis:

如果您正在使用XSLT转换,请尝试:

< xsl:choose>

< xsl:choose >

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

< xsl:当测试= " / /员工/ Emp " >

< -- Node exists-->

<——节点存在——>

< /xsl:when>

< / xsl:when >

< xsl:otherwise>

< xsl:否则>

< --Node does not exist-->

<——节点不存在——>

< /xsl:otherwise>

< / xsl:否则>

< /xsl:choose>

< / xsl:choose >

#9


0  

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

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

#10


0  

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.

getElementsByTagName["tagname"]也是一个DOM方法,可以用来获取节点。如果节点不匹配,则返回null。

#1


6  

With linq 2 xml in c#:

使用c#中的linq 2 xml:

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

#2


2  

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来转换此文档,那么我假设一个变量将提供最佳的功能。你会用这个:

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

这将给出名称节点的数量,您可以将其更改为任何您想要的。显然,如果它是零,那就没有,否则就是计数。

#3


2  

XmlDocument _xmlDoc = new XmlDocument();

_xmlDoc.Load(Server.MapPath("~/XMLFile.xml"));

XmlNode _node = _xmlDoc.SelectSingleNode("Employee/Emp");

if (_node != null)

{

    XmlNodeList _nodeList = _node.SelectNodes("Name");

    Response.Write(_nodeList.Count);
}

else

{

    Response.Write("Emp node doesnot exist");

}

#4


1  

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

如果您正在用Java编程,那么您应该查看两个相关的库。

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

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.

我现在将介绍Dom4j 2.0,因为它支持泛型、XPath,现在有了一些更好的高级支持。我认为Dom4j是从早期的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.

您可以从文件、URL、字符串等中读取XML,解析它,并仅在几行代码中检查节点。

#5


1  

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):

LINQ是伟大的。但万一你被困在。net 2的系统中。您可能需要使用“old”(XPath)方法(xmlFragment是您上面的XML字符串):

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

#6


0  

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.

这里至少有4个节点,假设您的与一个开放的 标记匹配: , ID,以及字符串" D "都将表示为节点。你的问题不清楚你是否真的想把所有这些都算上。我也不确定你是否想确定其中一个的存在。

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

但是,最终,XPath可能是您要寻找的。

#7


0  

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

作为XPath的替代品,许多具有XML DOM支持的语言将允许您在XML文档上调用方法,比如:

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.

XPath很好,但它更适合以有趣而复杂的方式轻松导航XML文档。这段代码将比相应的XPath代码简单一些。

#8


0  

If you are using XSLT transformation just trythis:

如果您正在使用XSLT转换,请尝试:

< xsl:choose>

< xsl:choose >

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

< xsl:当测试= " / /员工/ Emp " >

< -- Node exists-->

<——节点存在——>

< /xsl:when>

< / xsl:when >

< xsl:otherwise>

< xsl:否则>

< --Node does not exist-->

<——节点不存在——>

< /xsl:otherwise>

< / xsl:否则>

< /xsl:choose>

< / xsl:choose >

#9


0  

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

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

#10


0  

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.

getElementsByTagName["tagname"]也是一个DOM方法,可以用来获取节点。如果节点不匹配,则返回null。