如何使用vba修改xml声明 [英] How can I use vba to modify xml declarations
问题描述
我的第一篇文章,加油!在学习VBA并取得很大进步的同时,我已经潜伏了过去的6个月。但是,现在我陷入了困境,需要我的帮助!
my first post, woot! I've been lurking for the last 6 months as I have been learning VBA and have made a lot of progress. But now I'm stuck and I need your help!
我有一组xml文档,需要用另一个声明替换顶部的声明,并且不知道该怎么做。我已经阅读了许多有关在VBA中使用DOMDocument来修改xml文档的文章,并且我已经弄清楚了如何在Microsoft API的帮助下修改xml文件中的不同节点。但是我似乎无法访问声明。
I have a set of xml documents that I need to replace the declarations at the top with another declaration and I have no idea how to do this. I have read many articles on using DOMDocument in VBA to modify xml documents and I have figured out how to modify different nodes within the xml file with help of the microsoft API. However I cannot seem to access the declarations.
这可能吗?
如果可以,我在正确的轨道上吗?
If so, am I on the right track?
如果是的话,我需要调用什么(如果存在)属性或方法以访问xml文件中的声明,以便可以对其进行修改?
If so, what (if they exist) properties or methods do I need to call to access the declarations in the xml file so that I can modify them?
如果没有,我应该如何处理?
If not, how should I go about this?
我真的很想避免通过手工复制和粘贴来完成此操作,有成千上万的记录需要修改。
I would really like to avoid doing this by copy and pasting by hand, there are thousands of records that need modification.
这是我的VBA来提取和修改字段:
Here is my VBA to pull and modify fields:
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表单读取)。
My goal is to remove the declarations part of the xml file and replace it with another set of declarations that allow it to be read by infopath again (the current declarations will not be read by the infopath form).
我要删除的声明:
<?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文档:
Here is an xml document that I am trying to modify:
<?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>
推荐答案
所以我找到了答案:
您需要在此处进一步阅读API,以了解如何使用msxml插件: http://msdn.microsoft.com/en-us/library/ms766487
You need to read the API further here to learn how to use the msxml addon: http://msdn.microsoft.com/en-us/library/ms766487
在这一点上,我使用了VBA访问声明:
At which point I used this line of VBA to access the declarations:
objXML.ChildNodes.Item(0)
这是第一个声明:
<?xml version="1.0" encoding="UTF-8"?>
基本上,我需要学习DOM文档的结构以及节点及其属性的使用。希望这会帮助其他人将来寻找答案。
Basically I needed to learn about the structure of a DOM document and the use of nodes and their properties. Hopefully this will help others out there looking for this answer in the future.
这篇关于如何使用vba修改xml声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!