Bash脚本:使用xmllint将XML元素放入数组
这是我们的后续问题 我希望最后得到一个数组,其中包含xml的所有Bash脚本:使用xmllint将XML元素放入数组,xml,bash,shell,Xml,Bash,Shell,这是我们的后续问题 我希望最后得到一个数组,其中包含xml的所有元素 array[0] = "<![CDATA[A title for .... <br />]]>" array[1] = "<![CDATA[A title for .... <br />]]>" 数组[0]=“]]>” 数组[1]=“]]>” file.xml: http://www.foobar.com/foo/bar http://bar.com/foo http:/
元素
array[0] = "<![CDATA[A title for .... <br />]]>"
array[1] = "<![CDATA[A title for .... <br />]]>"
数组[0]=“]]>”
数组[1]=“]]>”
file.xml:
http://www.foobar.com/foo/bar
http://bar.com/foo
http://myurl.com/foo
http://desiredURL.com/files/ddd
http://asdasd.com/onefile/g.html
http://second.com/link
]>
...
ABash
解决方案可能是
let itemsCount=$(xmllint --xpath 'count(//item/description)' /tmp/so.xml)
declare -a description=( )
for (( i=1; i <= $itemsCount; i++ )); do
description[$i]="$(xmllint --xpath '//item['$i']/description' /tmp/so.xml)"
done
echo ${description[@]}
返回每个
内容为什么要将它们存储在数组中?我想从每个数组对象中提取值。对于阵列,您会建议什么更好的解决方案?什么是Xpath表达式?谢谢!问题是,您在免责声明中描述的内容在一个字符串中返回描述元素的所有内容。这对于进一步处理来说不是太有用。免责声明之前的代码应该按照您的要求执行。这里的免责声明仅适用于您不关心/不需要用作源代码的元素描述的情况。我认为在bash解决方案中,它应该是“smallerThanOrEqualTo”
let itemsCount=$(xmllint --xpath 'count(//item/description)' /tmp/so.xml)
declare -a description=( )
for (( i=1; i <= $itemsCount; i++ )); do
description[$i]="$(xmllint --xpath '//item['$i']/description' /tmp/so.xml)"
done
echo ${description[@]}
xmllint --xpath '//item/description/text()' /tmp/so.xml