2012-07-24 95 views
0

我的第一篇文章,woot!过去6个月我一直在潜伏,因为我一直在学习VBA并取得了很多进展。但现在我卡住了,我需要你的帮助!如何使用vba修改xml声明

我有一套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中再次被读取的替换(目前的声明不会被信息表格读取)。

的声明我想删除:

<?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

将声明总是相同吗?即相同的长度,并始终在第一? – Romain 2012-07-24 09:30:19

+0

是的,他们会.. – Ceekay 2012-07-25 17:07:39

+0

我将VB代码留给了解VB的人,但值得指出的是(“我有成千上万的文本文件,我需要对公式中的几行进行公式化修改他们每个人“)通常是一种Perl/bash/awk单线程最能解决的问题。交叉训练是非常有益的。 – 2012-07-28 05:36:28

回答

1

所以,我找到了答案:

您需要在此处进一步阅读API以了解如何使用msxml插件:http://msdn.microsoft.com/en-us/library/ms766487

在这一点我用这条线VBA的访问声明:

objXML.ChildNodes.Item(0) 

这是firt声明:

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

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