解析从Curl接收的XML以获取特定的值

时间:2021-02-14 23:48:41

I'm trying to filter this code for the data between <cookie> & </cookie> and the data between account-id=" & " (the trailing quote)

我正在过滤 & 之间的数据和account-id=" & "之间的数据(后面的引用)

<?xml version="1.0" encoding="utf-8"?>
<results>
 <status code="ok"/>
 <common locale="en" time-zone-id="85">
  <cookie>na3breezfxm5hk6co2kfzuxq</cookie>
  <date>2012-11-11T16:26:52.713+00:00</date>
  <host>http://meet97263421.adobeconnect.com</host>
  <local-host>pacna3app09</local-host>
  <admin-host>na3cps.adobeconnect.com</admin-host>
  <url>/api/xml?action=common-info</url>
  <version>8.2.2.0</version>
  <tos-version>7.5</tos-version>
  <product-notification>true</product-notification>
  <account account-id="1013353222"/>
  <user-agent>curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5</user-agent>
 </common>
</results>

Any help would be appreciated.

如有任何帮助,我们将不胜感激。

EDIT

编辑

This is the curl command I run to return the above xml.

这是我运行的curl命令来返回上面的xml。

curl -s http://meet97263421.adobeconnect.com/api/xml?action=common-info

3 个解决方案

#1


5  

In general, regexes (and therefore grep) aren't well-suited to parsing XML, but if you can guarantee the input is well-formatted and consistent you can do this most easily with grep's perl-style regexes (on systems whose grep has them):

一般来说,regex(因此grep)并不适合解析XML,但是如果您可以保证输入格式良好且一致,那么您可以很容易地使用grep的perl样式的regexes(在grep拥有它们的系统上):

grep -oP '(?<=<cookie>).*?(?=</cookie>)'
grep -oP '(?<=account-id=").*?(?=")'

If you want them in the same command, you can separate them with a |, but then you have to tell which matches which.

如果你想让它们在同一个命令中,你可以用|把它们分开,但是你必须告诉它们哪个匹配。

grep -oP '(?<=<cookie>).*?(?=</cookie>)|(?<=account-id=").*?(?=")'

#2


3  

As stated by @Kevin regular expressions are ill-suited to parsing XML.

正如@Kevin所说,正则表达式不适合解析XML。

A better approach is to use the xmllint program which apply an xpath expression as follows:

更好的方法是使用xmllint程序,它应用xpath表达式如下:

$ xmllint --xpath "string(/results/common/cookie)" data.xml
na3breezfxm5hk6co2kfzuxq

$ xmllint --xpath "string(/results/common/account/@account-id)" data.xml
1013353222

#3


0  

Use these XPath expressions

使用这些XPath表达式

/results/common/cookie

/results/common/account/@account-id

with a command line XPath interpreter

使用命令行XPath解释器。

#1


5  

In general, regexes (and therefore grep) aren't well-suited to parsing XML, but if you can guarantee the input is well-formatted and consistent you can do this most easily with grep's perl-style regexes (on systems whose grep has them):

一般来说,regex(因此grep)并不适合解析XML,但是如果您可以保证输入格式良好且一致,那么您可以很容易地使用grep的perl样式的regexes(在grep拥有它们的系统上):

grep -oP '(?<=<cookie>).*?(?=</cookie>)'
grep -oP '(?<=account-id=").*?(?=")'

If you want them in the same command, you can separate them with a |, but then you have to tell which matches which.

如果你想让它们在同一个命令中,你可以用|把它们分开,但是你必须告诉它们哪个匹配。

grep -oP '(?<=<cookie>).*?(?=</cookie>)|(?<=account-id=").*?(?=")'

#2


3  

As stated by @Kevin regular expressions are ill-suited to parsing XML.

正如@Kevin所说,正则表达式不适合解析XML。

A better approach is to use the xmllint program which apply an xpath expression as follows:

更好的方法是使用xmllint程序,它应用xpath表达式如下:

$ xmllint --xpath "string(/results/common/cookie)" data.xml
na3breezfxm5hk6co2kfzuxq

$ xmllint --xpath "string(/results/common/account/@account-id)" data.xml
1013353222

#3


0  

Use these XPath expressions

使用这些XPath表达式

/results/common/cookie

/results/common/account/@account-id

with a command line XPath interpreter

使用命令行XPath解释器。