VBA IXMLDOMNode.hasChildNodes对我来说无法正常工作(VBA IXMLDOMNode.hasChildNodes not working correctly for me)

编程入门 行业动态 更新时间:2024-10-20 07:40:33
VBA IXMLDOMNode.hasChildNodes对我来说无法正常工作(VBA IXMLDOMNode.hasChildNodes not working correctly for me)

这似乎应该很容易,但它没有按预期工作。 首先,我将发布XML,然后发布我拥有的VBA。 不幸的是,我无法发布整个XML文件。

-<item id="ref4"> <paratext>Reinstall tire retainers, if applicable.</paratext> </item>- <item>- <paratext> Repeat steps <xref xrefid="ref3"/> . through <xref xrefid="ref4"/>. for remaining wheel.</paratext> </item>- <item> <paratext>Apply thick coat of grease to wheel shafts.</paratext> </item>- <item> <paratext>Remove gloves and then goggles.</paratext> </item>
Dim xmlDoc As New DOMDocument
Dim n As IXMLDOMNode
'ProcSteps
    For Each n In xmlDoc.selectNodes("//procsteps/seqlist/item/paratext")
Debug.Print strSYSCOM, n.hasChildNodes, n.Text
        If n.hasChildNodes = False Then ' does this step reference other steps? If so, we don't need to process it.
            If HasNumber(n.Text) Then
                rsSteps.AddNew
                    rsSteps!MRC_ID = lngMRCID
                    rsSteps!txtStep = n.Text
                rsSteps.Update
            End If
        End If
    Next
End If
 

所以我基本上要确定的是paratext标签中是否存在外部参照标签。 如果他们这样做,那么我不想处理paratext标签。 而且,我需要尽可能高效地完成这项工作,因为我需要处理数千个XML文档。

当我打印如上所示的n.text时,我明白了

重复步骤。 通过。 留下轮子。

所以在我看来,xref不是paratext的子节点。 事实上,我还没有遇到过haschildnodes错误的情况。 那么,我错过了什么? 大卫

This seems like it should be easy, but it is not working as expected. First, I will post the XML and then the VBA that I have. Unfortunately, I cannot post the entire XML file.

-<item id="ref4"> <paratext>Reinstall tire retainers, if applicable.</paratext> </item>- <item>- <paratext> Repeat steps <xref xrefid="ref3"/> . through <xref xrefid="ref4"/>. for remaining wheel.</paratext> </item>- <item> <paratext>Apply thick coat of grease to wheel shafts.</paratext> </item>- <item> <paratext>Remove gloves and then goggles.</paratext> </item>
Dim xmlDoc As New DOMDocument
Dim n As IXMLDOMNode
'ProcSteps
    For Each n In xmlDoc.selectNodes("//procsteps/seqlist/item/paratext")
Debug.Print strSYSCOM, n.hasChildNodes, n.Text
        If n.hasChildNodes = False Then ' does this step reference other steps? If so, we don't need to process it.
            If HasNumber(n.Text) Then
                rsSteps.AddNew
                    rsSteps!MRC_ID = lngMRCID
                    rsSteps!txtStep = n.Text
                rsSteps.Update
            End If
        End If
    Next
End If
 

So basically what I am trying to determine is whether or not xref tags exist within the paratext tags. If they do, then I don't want to process the paratext tags. And, I need to do this as efficiently as possible because I have thousands of XML documents to process.

When I print n.text as shown above, I get

Repeat steps . through . for remaining wheel.

So it seems to me that xref is not a child node of paratext. And in fact, I haven't run across a situation yet where haschildnodes has been false. So, what am I missing? David

最满意答案

子节点不是子元素 。 每个元素的文本值也被视为“节点”。

为什么不简单地使用XPATH来做到这一点?

Sub foo()

Dim xml_string As String
Dim n As Object 'IXMLDomNode
Dim c as Object 'IXMLDomNode
Dim nodes As Object 'IXMLDomNodeList
Dim xmlDoc As Object 'MSXML2.DomDocument

xml_string = "<paratext>Repeat steps" & _
      "<xref xrefid='ref3'/>" & _
      " .through" & _
      "<xref xrefid='ref4'/>." & _
      "  for remaining wheel.</paratext>"


Set xmlDoc = CreateObject("MSXML2.DomDocument")

xmlDoc.LoadXML xml_string

Set nodes = xmlDoc.SelectNodes("//paratext")

For Each n In nodes
    If n.SelectNodes("//xref") Is Nothing Then
        'Process this PARATEXT node
        MsgBox "process!"
    Else
        'There ARE child nodes of /xref tagname, so skip this node
        'do nothing
        MsgBox "/xref child nodes exist, not processed!" & vbCrLf & vbCrLf & n.XML

    End If

Next

End Sub
 

注意,如果xpath参数指定的节点不存在:

Set nodes = xmlDoc.SelectNodes("//paratext/Hulk_Hogan")
 

For / Each循环将跳过,因为nodes NodeList将是一个空集合。 您甚至可以通过执行nodes.Length来测试这个(如果您愿意),当没有匹配的节点时,它将返回0值。

A child node is not a child element. The text values of each element are also considered "nodes".

Why not simply use the XPATH to do this?

Sub foo()

Dim xml_string As String
Dim n As Object 'IXMLDomNode
Dim c as Object 'IXMLDomNode
Dim nodes As Object 'IXMLDomNodeList
Dim xmlDoc As Object 'MSXML2.DomDocument

xml_string = "<paratext>Repeat steps" & _
      "<xref xrefid='ref3'/>" & _
      " .through" & _
      "<xref xrefid='ref4'/>." & _
      "  for remaining wheel.</paratext>"


Set xmlDoc = CreateObject("MSXML2.DomDocument")

xmlDoc.LoadXML xml_string

Set nodes = xmlDoc.SelectNodes("//paratext")

For Each n In nodes
    If n.SelectNodes("//xref") Is Nothing Then
        'Process this PARATEXT node
        MsgBox "process!"
    Else
        'There ARE child nodes of /xref tagname, so skip this node
        'do nothing
        MsgBox "/xref child nodes exist, not processed!" & vbCrLf & vbCrLf & n.XML

    End If

Next

End Sub
 

Observe that if the nodes specified by the xpath parameter do not exist:

Set nodes = xmlDoc.SelectNodes("//paratext/Hulk_Hogan")
 

The For / Each loop will skip, since the nodes NodeList will be an empty collection. You can even test for this (if you want) by doing nodes.Length which will return 0 value when there are no matching nodes.

更多推荐

本文发布于:2023-07-06 08:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对我来说   无法正常   工作   IXMLDOMNode   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!