leggewie
leggewie

Reputation: 272

how to extract data from XML with xidel

I'm getting my feet wet with xidel and want to use it together with namesilo.com API for updating DNS records. I'm having trouble constructing the right selector. Let's say, I had the following xml response, how would I go about selecting the record_id for host www.mydomain.org?

<?xml version="1.0"?>
<namesilo>
  <request>
    <operation>dnsListRecords</operation>
    <ip>62.157.5.106</ip>
  </request>
  <reply>
    <code>300</code>
    <detail>success</detail>
    <resource_record>
      <record_id>7e1abd117be5506febe327ab906f67c7</record_id>
      <type>A</type>
      <host>www.mydomain.org</host>
      <value>182.245.2.23</value>
      <ttl>172817</ttl>
      <distance>0</distance>
    </resource_record>
    <resource_record>
      <record_id>7e75694e3da869315b92d386dcbed45b</record_id>
      <type>A</type>
      <host>m.mydomain.org</host>
      <value>21.148.13.45</value>
      <ttl>172817</ttl>
      <distance>0</distance>
    </resource_record>
  </reply>
</namesilo>

I haven't gotten past xidel --extract //resource_record, really. All attempts at //resource_record[host="www.mydomain.org"]/record_id and similar have failed so far. Piping through grep and sed would work via xidel --extract //resource_record | grep www.mydomain.org | sed s/www.mydomain.org.*// on the raw, unprettified XML-response from namesilo.com, I guess, but I'm sure there is a better way.

Upvotes: 1

Views: 498

Answers (2)

Reino
Reino

Reputation: 3433

Does not work: xidel -e //resource_record[host="www.mydomain.org"]/record_id

Generally speaking it's recommended to quote a (extraction) query. That doesn't mean it won't work without quotes:

xidel -s <input> -e //resource_record\[host=\"www.mydomain.org\"\]/record_id
7e1abd117be5506febe327ab906f67c7

It's just that you have to prevent certain characters from being interpreted by Bash's shell by escaping them.

Upvotes: 1

leggewie
leggewie

Reputation: 272

Does not work: xidel -e //resource_record[host="www.mydomain.org"]/record_id

Works: xidel -e '//resource_record[host="www.mydomain.org"]/record_id'

Upvotes: 0

Related Questions