如何使用vba修改xml声明

如何使用vba修改xml声明,xml,vba,declaration,Xml,Vba,Declaration,我的第一篇帖子,呜!在过去的6个月里,我一直在潜行,因为我一直在学习VBA,并取得了很大的进步。但现在我陷入困境,我需要你的帮助 我有一组xml文档,我需要用另一个声明替换顶部的声明,我不知道如何做。我已经阅读了许多关于在VBA中使用DOMDocument修改xml文档的文章,并且已经了解了如何在microsoft API的帮助下修改xml文件中的不同节点。然而,我似乎无法访问声明 这可能吗 如果是这样,我走对了吗 如果是,我需要调用什么(如果存在)属性或方法来访问xml文件中的声明,以便修改它

我的第一篇帖子,呜!在过去的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>

MikeTest2
2011-12-07
营销
HermanTest3
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
发送给团队的电子邮件,鼓励对隔间内的地板空间进行全面清理。
所以我找到了答案:

您需要在此处进一步阅读API,以了解如何使用msxml插件:

此时,我使用这行VBA访问声明:

objXML.ChildNodes.Item(0)
这是第一份声明:

<?xml version="1.0" encoding="UTF-8"?> 


基本上,我需要了解DOM文档的结构以及节点及其属性的使用。希望这能帮助其他人在将来寻找这个答案。

声明是否总是一样的?ie的长度相同,总是排在第一位?我将把VB代码留给了解VB的人,但值得指出的是,“我有数千个文本文件,我需要对每个文件中的几行进行公式化更改”)通常是最好用Perl/bash/awk单行程序解决的问题。交叉训练是非常有益的。