我的第一篇文章,哇!在过去的6个月里,我一直潜伏着,因为我一直在学习VBA并取得了很大的进步。但现在我被困住了,我需要你的帮助!
我有一组xml文档,我需要用另一个声明替换顶部的声明,我不知道如何做到这一点。我已经阅读了很多关于在VBA中使用DOMDocument来修改xml文档的文章,并且我已经找到了如何借助microsoft API修改xml文件中的不同节点。但是我似乎无法访问声明。
这可能吗?
如果是这样,我是否走在正确的轨道上?
如果是这样,我需要调用什么(如果存在)属性或方法来访问xml文件中的声明,以便我可以修改它们?
如果没有,我应该怎么做呢?
我真的想通过手工复制和粘贴来避免这样做,有数千条需要修改的记录。
这是我的VBA来拉取和修改字段:
Sub XMLtest()
Dim strXML As String
Dim objXML As MSXML2.DOMDocument
Dim point As IXMLDOMNode
Dim origVal As String
Dim newVal As String
Set objXML = New MSXML2.DOMDocument
strXML = "Q:\TIS\!Safety(Beth)\Tim's Projects\Safety Inspections\xml\2011-12-07T10_32_31(good tag).xml"
objXML.Load (strXML)
Set point = objXML.DocumentElement.ChildNodes.Item(0)
Debug.Print point.XML
origVal = objXML.DocumentElement.ChildNodes.Item(0).Text
MsgBox origVal
'this is a section that will change the value of the first item
objXML.DocumentElement.ChildNodes.Item(0).Text = "TimTest1"
MsgBox objXML.DocumentElement.ChildNodes.Item(0).Text
objXML.Save (strXML)
End Sub
我的目标是删除xml文件的声明部分,并将其替换为允许再次由infopath读取的另一组声明(infopath表单不会读取当前声明)。
我要删除的声明:
<?xml version="1.0" encoding="UTF-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Office-Inspection:-myXSD-2011-10-05T14-49-56" PIVersion="1.0.0.0" href="http://a.octer.com/dept/TIS/TISSafety/Office%20Inspection/Forms/template.xsn" solutionVersion="1.0.0.31" productVersion="14.0.0" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-10-05T14:49:56" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
这是我想要修改的xml文档:
<?xml version="1.0" encoding="UTF-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Office-Inspection:-myXSD- 2011-10-05T14-49-56" PIVersion="1.0.0.0" href="http://a.octer.com/dept/TIS/TISSafety/Office%20Inspection/Forms/template.xsn" solutionVersion="1.0.0.31" productVersion="14.0.0" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-10-05T14:49:56" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:Manager>MikeTest2</my:Manager>
<my:Date>2011-12-07</my:Date>
<my:Team>Marketing</my:Team>
<my:Inspector>HermanTest3</my:Inspector>
<my:Aisles>Y</my:Aisles>
<my:SecondaryAisles>Y</my:SecondaryAisles>
<my:CircutOverload>Y</my:CircutOverload>
<my:OutletCovers>Y</my:OutletCovers>
<my:PanelsClosed>Y</my:PanelsClosed>
<my:FireExt>Y</my:FireExt>
<my:ExtTag>Y</my:ExtTag>
<my:CeilingTiles>Y</my:CeilingTiles>
<my:Sprinklers>Y</my:Sprinklers>
<my:Evacuation>Y</my:Evacuation>
<my:a44444>Y</my:a44444>
<my:Comments>email sent to team encouraging a general cleanup of floor space in cubicles.</my:Comments>
</my:myFields>
答案 0 :(得分:2)
所以我找到了答案:
您需要在此处进一步阅读API以了解如何使用msxml插件:http://msdn.microsoft.com/en-us/library/ms766487
此时我使用这行VBA来访问声明:
objXML.ChildNodes.Item(0)
这是第一个声明:
<?xml version="1.0" encoding="UTF-8"?>
基本上我需要了解DOM文档的结构以及节点及其属性的使用。希望这将有助于其他人在将来寻找这个答案。