Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本:使用xmllint将XML元素放入数组_Xml_Bash_Shell - Fatal编程技术网

Bash脚本:使用xmllint将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:/

这是我们的后续问题

我希望最后得到一个数组,其中包含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://myurl.com/foo
http://desiredURL.com/files/ddd
http://asdasd.com/onefile/g.html
http://second.com/link
]> ...
A
Bash
解决方案可能是

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