Show HN: Small tool to query XML data using XPath

4 months ago 5

Query XML data using XPath.

go install github.com/linkdd/xq@latest

Dump the whole XML:

virsh net-dumpxml example | xq "/"
<?xml version="1.0"?><network connections="1"> <name>example</name> <uuid>00000000-0000-0000-0000-000000000000</uuid> <forward mode="nat"> <nat> <port start="1024" end="65535"></port> </nat> </forward> <bridge name="virbr1" stp="on" delay="0"></bridge> <mac address="52:54:00:7c:52:01"></mac> <domain name="example.local" localOnly="yes"></domain> <dns enable="yes"></dns> <ip family="ipv4" address="10.1.1.1" prefix="24"> <dhcp> <range start="10.1.1.2" end="10.1.1.254"></range> <host mac="52:54:00:96:7a:ed" name="example-node-0" ip="10.1.1.10"></host> </dhcp> </ip> </network>

Dump a specific node:

virsh net-dumpxml example | xq "/network/ip"
<ip family="ipv4" address="10.1.1.1" prefix="24"> <dhcp> <range start="10.1.1.2" end="10.1.1.254"></range> <host mac="52:54:00:96:7a:ed" name="example-node-0" ip="10.1.1.10"></host> </dhcp> </ip>

Dump an attribute:

virsh net-dumpxml example | xq "/network/ip/@address"

Dump multiple nodes:

virsh dumpxml example-node-0 | xq "/domain/cpu/feature"
<feature policy="require" name="x2apic"></feature> <feature policy="require" name="hypervisor"></feature> <feature policy="require" name="lahf_lm"></feature> <feature policy="disable" name="svm"></feature>

This software is released under the terms of the MIT License.

Read Entire Article