我有客户提供的以下 XML,我需要从中提取如下项目:
<CustomerProductName> <ProductName> <ProductAssetName> 处理每个 <CustomerProducts>部分作为单独的订单。
我可以去 <CustomerProducts>并提取 <CustomerProductName>没有问题并迭代每个部分中的值。
但我看不出我是如何得到它下面的值的。如果有人可以给我一些关于如何实现这一目标的指示,我将不胜感激,因为我已经尝试了一天多了。
基本代码在XML下面
<?xml version="1.0"?>
<Products_Root>
<Products_IPN VendorID="11344" >
<CustomerProducts CustomerProductName="Test" ProductCount="7">
<Products ProductType="BOOK" ProductName="Donald" >
<ProductComponents ProductAssetName="Donald.pdf" />
<ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
<ProductSpecs SpecClass="OPERATION" SpecType="BIND" SpecValue="SADDLE"/>
</Products>
<Products ProductType="BOOK" ProductName="Duck">
<ProductComponents ProductAssetName="Duck.pdf"/>
<ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
</Products>
</CustomerProducts>
<CustomerProducts CustomerProductName="Test2" ProductCount="2">
<Products ProductType="BOOK" ProductName="Micky">
<ProductComponents ProductAssetName="Mouse.pdf" />
<ProductComponents ProductAssetName="Mouse.pdf" />
<ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
</Products>
<CustomerProductSpecs SpecClass="OPERATION" SpecType="KITTING" SpecValue="SHRINKWRAP KIT"/>
</CustomerProducts>
<CustomerProducts CustomerProductName="Test3" ProductCount="6">
<Products ProductType="BOOK" ProductName="Minnie">
<ProductComponents ProductAssetName="Mouse" />
<ProductSpecs SpecClass="MEASUREMENT" SpecType="MARGINS" SpecValue="PER FILE"/>
</Products>
</CustomerProducts>
</Products_IPN>
</Products_Root>
这是我到目前为止的 VBScript 代码
Dim xmlDoc, objNodeList, plot
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("E:\dropbox\Dropbox\Hobbs\Xerox Example Files\test.xml")
Set objNodeList = xmlDoc.getElementsByTagName("CustomerProducts")
plot="No Value"
If objNodeList.length > 0 then
For each x in objNodeList
JobName=x.getattribute("CustomerProductName")
msgbox JobName
Next
Else
msgbox chr(34) & "CustomerProducts" & chr(34) & " field not found."
End If
最佳答案
您已经将选择语言设置为 XPath,也许您也应该使用它。 :)
Option Explicit
Dim xmlDoc, CustomerProducts, Products
Dim plot, CustomerProductName, ProductName
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load "products.xml"
plot = "No Value"
For Each CustomerProducts In xmlDoc.SelectNodes("//CustomerProducts")
CustomerProductName = CustomerProducts.getAttribute("CustomerProductName")
For Each Products In CustomerProducts.SelectNodes("./Products")
ProductName = Products.getAttribute("ProductName")
MsgBox CustomerProductName & " - " & ProductName
Next
Next
关于xml - VBScript 遍历 XML 子节点并检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12075314/
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token
我们有一个字符串:“”这个正则表达式://i如何从当前字符串中获取所有匹配项? 最佳答案 "".scan(//)参见scan在ruby-docs上 关于ruby-如何遍历Ruby中所有正则表达式匹配的字符串?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6857852/
我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::
我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi
这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::
我想从0到2循环@a:0,1,2,0,1,2。defset_aif@a==2@a=0else@a=@a+1endend也许有更好的方法? 最佳答案 (0..2).cycle(3){|x|putsx}#=>0,1,2,0,1,2,0,1,2item=[0,1,2].cycle.eachitem.next#=>0item.next#=>1item.next#=>2item.next#=>0... 关于ruby-循环遍历数组的元素,我们在StackOverflow上找到一个类似的问题: