Command Line XML Parsing (Ubuntu/Linux)

To read xml document with terminal we can use xmllint tool.

Lets say we have a file named 1.xml with the below content:

<?xml version="1.0" encoding="utf-8"?>
<item>
  <definition test="abc">v. to provide a place to sleep or live</definition>
  <definition>def2</definition>
  <word>accommodates</word>
</item>

Get elements:

xmllint --xpath "/item/definition" 1.xml

Output: <definition test="abc">v. to provide a place to sleep or live</definition><definition>def2</definition>

Get text of elements:

xmllint --xpath "//item/word/text()" 1.xml

Output: accommodates

Get attributes:

xmllint --xpath "/item/definition/@test" 1.xml

Output:  test="abc"

Select elements by its attribute:

xmllint --xpath "/item/definition[@test='abc']" 1.xml

Output: <definition test="abc">v. to provide a place to sleep or live</definition>


Search element by part of text:

xmllint --xpath "//*[contains(*,'sleep')]" 1.xml

Search element by part of attribute:

xmllint --xpath "//*[contains(@test,'bc')]" 1.xml

Parse HTML URL:

xmllint --html --xpath //div/a  "http://blog.mbirgin.com"

Parse HTML and ignore error messages: 

curl -s http://www.mbirgin.com | xmllint --html  --xpath '(//td[contains(@class,"lastpost") and contains(@class,"windowbg2")])[1]/text()'  2>/dev/null - 

Share Share Editor: editor | Posted: 2014/10/08 | Views: 64212

Comments

9 + 8 =
Fitch 11/24/2023 12:07:31
payday loan
cash advance online
no credit check payday loans instant approval
personal online loans
loans for bad credit
Stringfield 11/17/2023 10:42:50
casino online for real money
casino real money
real money slots
real money online casino
real money slots
Seabrook 11/17/2023 03:43:48
generic viagra online for sale
generic name for viagra
viagra without doctor’s prescription
viagra online canadian pharmacy
viagra generic online
Home | Search | Contact | Terms
Editor