0

I have read this answer: https://stackoverflow.com/a/7052168/6557127 , but my XML file is a bit different (openHAB REST API):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
 <item>
    <type>GroupItem</type>
    <name>All</name>
    <state>baz</state>
    <link>http://localhost:8080/rest/items/All</link>
 </item>
 <item>
    <type>GroupItem</type>
    <name>foo</name>
    <state>bar</state>
    <link>http://localhost:8080/rest/items/foo</link>
 </item>
</items>

How can I get the state of item foo in bash?

Community
  • 1
  • 1
Motte001
  • 191
  • 7
  • BTW -- if there are any `xmlns=` declarations up by the header of your document, that changes the semantics; be sure to include them. – Charles Duffy Aug 07 '16 at 04:13

1 Answers1

1

With XMLStarlet:

xmlstarlet sel -t -m "//item[name='foo']/state" -v .

...or, with Python 2.7 (here, called from a shell function):

get_state() {
  python -c '
import xml.etree.ElementTree as ET
import sys

doc = ET.parse(sys.stdin)
el = doc.find(".//item[name=\"%s\"]/state" % (sys.argv[1],))
if el is not None:
    print el.text
' "$@"
}

...used as:

foo_state=$(get_state foo <your.xml)

In either of cases, we're using a real XML parser (rather than trying to hack something together that doesn't really understand the syntax), and leveraging the XPath language to construct our actual query.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Can I directly use the webpage without downloading it? I don't want so many writes on the sd card of a pi – Motte001 Aug 07 '16 at 04:29
  • That said, you should probably have a shmfs (or other memory-backed) filesystem for `/tmp` if that's something you're worried about. – Charles Duffy Aug 07 '16 at 04:30