这似乎应该很容易,但它没有按预期工作。 首先,我将发布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 IfSo 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 SubObserve 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.
更多推荐
发布评论