CustomXMLNode.HasChildNodes 方法 (Office)
如果当前元素节点具有子元素节点,则获取 True 。
语法
表达式。HasChildNodes
表达 返回 CustomXMLNode 对象的表达式。
返回值
布尔值
备注
当 CustomXMLNode 不是节点类型 msoCustomXMLNodeElement 时,此方法将始终返回 False。
示例
以下示例演示如何使用各种方法来添加自定义 XML 部件、选择具有不同条件的部件和节点、追加子树、测试子树是否已成功添加以及删除部件和节点。
Sub ShowCustomXmlParts()
On Error GoTo Err
Dim cxps As CustomXMLParts
Dim cxp1 As CustomXMLPart
Dim cxp2 As CustomXMLPart
Dim cxn As CustomXMLNode
Dim cxns As CustomXMLNodes
Dim strXml As String
Dim strUri As String
With ActiveDocument
' Example written for Word.
' Adding a custom XML part.
.CustomXMLParts.Add "<custXMLPart />"
' Add and then load from a file.
Set cxp1 = .CustomXMLParts.Add
cxp1.Load "c:\invoice.xml"
' Returns the first custom XML part with the given root namespace.
Set cxp2 = .CustomXMLParts("urn:invoice:namespace") '
' Access all with a given root namespace; returns the entire collection.
Set cxps = .CustomXMLParts.SelectByNamespace("urn:invoice:namespace")
' DOM-type operations.
' Get the XML.
strXml = cxp2.XML
' Get the root namespace.
strUri = cxp2.NamespaceURI
' Get nodes using XPath.
Set cxn = cxp2.SelectSingleNode("//*[@quantity < 4]")
Set cxns = cxp2.SelectNodes("//*[@unitPrice > 20]")
' Append a child subtree to the single node selected previously.
cxn.AppendChildSubtree("<discounts><discount>0.10</discount></discounts>")
' Tell user that the child subtree was added.
If cxn.HasChildNodes then
MsgBox("The 'Discounts' nodes were added.")
Else
MsgBox("The 'Discounts' nodes were not added.")
End If
' Delete custom XML part and node and its children.
cxp2.Delete
cxn.Delete
End With
Exit Sub
' Exception handling. Show the message and resume.
Err:
MsgBox (Err.Description)
Resume Next
End Sub
另请参阅
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈