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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/653014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:24:27  来源:igfitidea点击:

How can I check whether a node exists in an XML file?

xml

提问by kumar

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>

回答by eglasius

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

回答by Stephen Friederichs

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 Nameand you can change that to anything you'd like. Obviously if it's zero then there are none, otherwise it's the count.

这将为您提供节点数,Name您可以将其更改为您想要的任何内容。显然,如果它是零,那么就没有,否则就是计数。

回答by Neha

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");

}

回答by jottos

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,解析它并仅用几行代码检查节点。

回答by rasx

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.x 的系统上,您可能必须使用“旧”(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}

回答by rasx

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

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

回答by Pramodh

If you are using XSLT transformation just trythis:

如果您正在使用 XSLT 转换,请尝试以下操作:

< xsl:choose>

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

< -- Node exists-->

< /xsl:when>

< xsl:otherwise>

< --Node does not exist-->

< /xsl:otherwise>

< /xsl:choose>

<xsl:选择>

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

< -- 节点存在-->

< /xsl:when>

< xsl:否则>

<--节点不存在-->

</xsl:否则>

</xsl:选择>

回答by Dan Breslau

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 allof these. I'm also not sure whether you want to determine the existence of a specific one of them.

此处至少有 4 个节点,假设您</Emp>与开始<Emp>标记匹配: <Emp>, <Name>, ID,并且字符串" D "将全部表示为节点。从您的问题中不清楚您是否真的想要计算所有这些。我也不确定您是否要确定其中一个特定的存在。

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

不过,归根结底,XPath 可能正是您要寻找的。

回答by Aaron

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 代码更直接一些。

回答by Nayana Setty

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 的方法。