我有一个要解析的 XML 文件,其内容正是下面的 XML:
<Results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Reference>{REFERENCE-HERE}</Reference>
<FillerTags>Filler</FillerTags>
<entity>
<entityName>ABC</entityName>
<entityId>012345</entityId>
</entity>
<Items>
<Item>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</Item>
<AnotherItem>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</AnotherItem>
</Items>
</Results>
我一直在努力使下面的代码(最初来 self 的 question here)起作用。其他几个用户(包括代码的创建者)已经能够成功地使用它,但是当我运行它时,输出文件只是以 ÿþ< 的形式出现。 .我确保将文件编码为 ANSI 并立即将记事本文件另存为 .xml,但输出仍然只有字节顺序标记。
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub ParseResults()
'Requires reference to Microsoft XML, v6.0
'Requires referenc to Microsoft Scripting Runtime
Dim xmlFilePath$, newFilePath$
Dim DOM As MSXML2.DOMDocument
Dim entity As IXMLDOMNode
Dim fso As Scripting.FileSystemObject
'# Define the file you are going to load as XML
xmlFilePath = "PATH"
'# Define an output path for where to put the modified XML
newFilePath = "NEWPATH"
'# Create our DOM object
Set DOM = CreateObject("MSXML2.DOMDocument")
'# Load the XML file
DOM.Load xmlFilePath
'# Wait until the Document has loaded
Do
Sleep 250
Loop Until DOM.readyState = 4
'# Get the entityID node
Set entity = DOM.DocumentElement.getElementsByTagName("entityId")(0)
'# Call a subroutine to append the entity to "Item" tags
AppendEntity DOM, "Item", entity
'# Call a subroutine to append the entity to "AnotherItem" tags
AppendEntity DOM, "AnotherItem", entity
'## Create an FSO to write the new file
Set fso = CreateObject("Scripting.FileSystemObject")
'## Attempt to write the new/modified XML to file
On Error Resume Next
fso.CreateTextFile(newFilePath, True, True).Write DOM.XML
If Err Then
'## Print the new XML in the Immediate window
Debug.Print DOM.XML
MsgBox "Unable to write to " & newFilePath & " please review XML in the Immediate window in VBE.", vbInformation
Err.Clear
End If
On Error GoTo 0
'Cleanup
Set DOM = Nothing
Set fso = Nothing
Set entity = Nothing
End Sub
Sub AppendEntity(DOM As Object, tagName As String, copyNode As Object)
'## This subroutine will append child node to ALL XML Nodes matching specific string tag.
Dim itemColl As IXMLDOMNodeList
Dim itm As IXMLDOMNode
'# Get a collection of all elements matching the tagName
Set itemColl = DOM.DocumentElement.getElementsByTagName(tagName)
'# Iterate over the collection, appending the copied node
For Each itm In itemColl
If itm.HasChildNodes Then
'# Insert this node before the first child node of Item
itm.InsertBefore copyNode.CloneNode(True), itm.FirstChild
Else
'# Append this node to the Item
itm.appendChild copyNode.CloneNode(True)
End If
Next
Set itm = Nothing
Set itemColl = Nothing
End Sub
可以肯定的是,代码没有产生任何错误——它创建了一个新文件,但它创建的文件不正确。正确的输出应该是(对于其他一些尝试过这段代码的人来说):
<Results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Reference>{REFERENCE-HERE}</Reference>
<FillerTags>Filler</FillerTags>
<entity>
<entityName>ABC</entityName>
<entityId>012345</entityId>
</entity>
<Items>
<Item>
<entityId>012345</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</Item>
<AnotherItem>
<entityId>012345</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</AnotherItem>
</Items>
即,代码将作为每个标签的子节点插入。在我希望将此代码用于此示例 XML 之后,我希望将其应用到的真实 XML 文档中,XML 大致相同,但包含多个实体。例如:
<Results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Reference>{REFERENCE-HERE}</Reference>
<FillerTags>Filler</FillerTags>
<entity>
<entityName>ABC</entityName>
<entityId>012345</entityId>
</entity>
<Items>
<Item>
<entityId>012345</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</Item>
<AnotherItem>
<entityId>012345</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</AnotherItem>
</Items>
<entity>
<entityName>DEF</entityName>
<entityId>678910</entityId>
</entity>
<Items>
<Item>
<entityId>678910</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</Item>
<AnotherItem>
<entityId>678910</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</AnotherItem>
</Items>
如果您能帮助解决此问题,我们将不胜感激。
更新:
上面的代码现在可以通过更改行 fso.CreateTextFile(newFilePath, True, True).Write DOM.XML 来工作至 fso.CreateTextFile(newFilePath, True, False).Write DOM.XML .
我现在正尝试在更大的 XML 数据集上运行它,但在 Set entity = DOM.DocumentElement.getElementsByTagName("entityId")(0) 行收到错误
我在示例文件中多次遇到此错误,然后意识到我只是忘记设置正确的目录,但尽管这次确保目录正确,但错误仍然存在。
更新 2:我收到此错误的代码已修改如下。如果我没记错的话,我所做的只是重命名了一些东西,但我可能错了。
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub ParseResults()
'Requires reference to Microsoft XML, v6.0
'Requires referenc to Microsoft Scripting Runtime
Dim xmlFilePath$, newFilePath$
Dim DOM As MSXML2.DOMDocument
Dim Customer As IXMLDOMNode
Dim fso As Scripting.FileSystemObject
'# Define the file you are going to load as XML
xmlFilePath = "C:\FAKEPATH\Final_Test.xml"
'# Define an output path for where to put the modified XML
newFilePath = "C:\FAKEPATH\Final_Test1.xml"
'# Create our DOM object
Set DOM = CreateObject("MSXML2.DOMDocument.6.0")
'# Load the XML file
DOM.Load xmlFilePath
'# Wait until the Document has loaded
Do
Sleep 250
Loop Until DOM.readyState = 4
'# Get the entityID node
Set Customer = DOM.DocumentElement.getElementsByTagName("CustomerId")(0)
'# Call a subroutine to append the entity to "Item" tags
AppendCustomer DOM, "Transaction", Customer
'## Create an FSO to write the new file
Set fso = CreateObject("Scripting.FileSystemObject")
'## Attempt to write the new/modified XML to file
On Error Resume Next
'MsgBox DOM.XML
fso.CreateTextFile(newFilePath, True, False).Write DOM.XML
If Err Then
'## Print the new XML in the Immediate window
Debug.Print DOM.XML
MsgBox "Unable to write to " & newFilePath & " please review XML in the Immediate window in VBE.", vbInformation
Err.Clear
End If
On Error GoTo 0
'Cleanup
Set DOM = Nothing
Set fso = Nothing
Set Customer = Nothing
End Sub
Sub AppendCustomer(DOM As Object, Transaction As String, copyNode As Object)
'## This subroutine will append child node to ALL XML Nodes matching specific string tag.
Dim itemColl As IXMLDOMNodeList
Dim itm As IXMLDOMNode
'# Get a collection of all elements matching the tagName
Set itemColl = DOM.DocumentElement.getElementsByTagName(Transaction)
'# Iterate over the collection, appending the copied node
For Each itm In itemColl
If itm.HasChildNodes Then
'# Insert this node before the first child node of Item
itm.InsertBefore copyNode.CloneNode(True), itm.FirstChild
Else
'# Append this node to the Item
itm.appendChild copyNode.CloneNode(True)
End If
Next
Set itm = Nothing
Set itemColl = Nothing
End Sub
更新 3:现在一切正常。唯一的问题在于上述代码执行的实际过程。由于存在多个实体,并且每组项目都属于一个实体,因此代码需要找到一个 entityId 并将此 entityId 应用于在另一个 entityId 标记出现之前出现的所有项目。在这一点之后,一切都会重复。
最佳答案
我最初把它放在这里作为答案,这样我就可以清楚地显示我的代码。如果这也失败,将删除。尝试此语法以使用写入文件的替代方法。 Notepadd++ 告诉我这是 ANSII:
'## Create an FSO to write the new file'
Set fso = CreateObject("Scripting.FileSystemObject")
Dim FF As Integer
FF = FreeFile
'## Attempt to write the new/modified XML to file'
fso.CreateTextFile newFilePath
Open newFilePath For Output As FF
Print #FF, dom.XML
Close #FF
或者
(同样,只是覆盖基础,如果需要会更新或删除)
尝试:
fso.CreateTextFile(newFilePath, True, False).Write DOM.XML
区别在于 CreateTextFile 方法中的第三个参数指定是将文件创建为 Unicode (True) 还是 ASCII (False) .
Notepad++ 确认此方法是 ANSII,而如果我执行 True 来创建 Unicode 文件,我会得到一个 UCS-2 Little Endian 文件。
我个人注意到 Ascii/Unicode 之间没有区别——我可以在 Notepad 或 Notepad++ 中打开它们,它们对我来说是一样的,但由于这似乎可能是字符编码问题,所以值得一试.我建议仅将其作为实现的第一个(也是最简单的)选项(如果需要,还有更多选项可供探索)。
更新 #3
为了解决文件的嵌套性质...基本上您有 XML 元素 siblings(“实体”和“项目”),您需要修改“项目”(以及它的子元素节点)以包含“entityId”(它是“entity”的子项)。我正在解释这种关系,希望这种修改有意义!
'##### NO LONGER USED:'
'# Get the entityID node'
'Set Customer = DOM.DocumentElement.getElementsByTagName("CustomerId")(0)'
Dim itm As IXMLDOMNode
'# Instead of getting the first item like we did before, we can iterate the collection'
' of nodes with the entityID tag like so:'
For Each Customer In DOM.DocumentElement.getElementsByTagName("entityId")
'Since Item is Entity nextSibling, and Entity is parent of entityId,'
' we can iterate the collection if its childNodes like this:'
For Each itm In Customer.ParentNode.NextSibling.ChildNodes
If itm.HasChildNodes Then
'# Insert this node before the first child node of Item'
itm.InsertBefore Customer.CloneNode(True), itm.FirstChild
Else
'# Append this node to the Item'
itm.appendChild Customer.CloneNode(True)
End If
Next
Next
'##### This function call is no longer needed
'AppendCustomer DOM, "Transaction", Customer'
这会生成如下 XML:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Results>
<Reference>{REFERENCE-HERE}</Reference>
<FillerTags>Filler</FillerTags>
<entity>
<entityName>ABC</entityName>
<entityId>012345</entityId>
</entity>
<Items>
<Item>
<entityId>012345</entityId>
<FillerTagsAgain>Filler1</FillerTagsAgain>
<FillerTagsAgain>Filler1</FillerTagsAgain>
<FillerTagsAgain>Filler1</FillerTagsAgain>
</Item>
<AnotherItem>
<entityId>012345</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</AnotherItem>
</Items>
</Results>
<Results>
<Reference>{REFERENCE-HERE}</Reference>
<FillerTags>Filler</FillerTags>
<entity>
<entityName>DEF</entityName>
<entityId>54321</entityId>
</entity>
<Items>
<Item>
<entityId>54321</entityId>
<FillerTagsAgain>Filler1</FillerTagsAgain>
<FillerTagsAgain>Filler1</FillerTagsAgain>
<FillerTagsAgain>Filler1</FillerTagsAgain>
</Item>
<AnotherItem>
<entityId>54321</entityId>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
<FillerTagsAgain>Filler2</FillerTagsAgain>
</AnotherItem>
</Items>
</Results>
</root>
关于XML 文件输出仅显示字节顺序标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23837027/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为