<Filer>
<ID>123456789</ID>
<Name>
<BusinessNameLine1>Stackoverflow</BusinessNameLine1>
</Name>
<NameControl>stack</NameControl>
<USAddress>
<AddressLine1>123 CHERRY HILL LANE</AddressLine1>
<City>LA</City>
<State>CA</State>
<ZIPCode>90210</ZIPCode>
</USAddress>
</Filer>下面是给我的xml代码示例。使用这个xml,我需要从这个xml中获取某个属性。
我只需要从文件中提取所有的<BusinessNameLine1>。问题是这个标记在整个文件中多次出现,但我只需要在<Filer>标记中它为假的情况下提取它。
我会这样做与PHP,但我在工作,我不能运行php代码,因为不能在我的电脑上安装软件。不过,我可以执行bash文件。这个文件也非常大,所以我不能把它放到excel中。我不知道该怎么做。我希望能得到一些帮助或指导,告诉我从哪里开始。
发布于 2014-06-03 00:25:03
使用适当的XML解析器。例如,xsh
open file.xml ;
ls //Filer//BusinessNameLine1 ;发布于 2014-06-03 00:26:15
xpath是您的朋友:有一个xmllint工具可以用来计算xpath
xmllint --xpath '//Filer//BusinessNameLine1/text()' yourXML输出:
Stackoverflow在使用<Filer>之外的<Busn..>标记的示例上进行测试
kent$ cat t.xml
<root>
<Trash>
<BusinessNameLine1>trash</BusinessNameLine1>
</Trash>
<Filer>
<ID>123456789</ID>
<Name>
<BusinessNameLine1>Stackoverflow</BusinessNameLine1>
</Name>
<NameControl>stack</NameControl>
<USAddress>
<AddressLine1>123 CHERRY HILL LANE</AddressLine1>
<City>LA</City>
<State>CA</State>
<ZIPCode>90210</ZIPCode>
</USAddress>
</Filer>
</root>
kent$ xmllint --xpath '//Filer//BusinessNameLine1/text()' t.xml
Stackoverflow发布于 2014-06-03 00:39:15
您可以尝试组合awk和sed命令,
$ awk -v RS='</Filer>' '/^<Filer>/ {gsub (/\n/," "); print}' file | sed -r 's/.*<BusinessNameLine1>([^<]*)<\/BusinessNameLine1>.*/\1/g'
Stackoverflowhttps://stackoverflow.com/questions/23998986
复制相似问题