Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data from xml file using xmllint

I have a small xml document from which I need to extract some values using xmllint. I am able to navigate through the xml hierarchy using xmllint --shell xmlfilename command.

But I am unable to extract the values. I don't want to use a grep / any pattern matching command, as that is already done and is a success.

I would appreciate any help regarding the xmlliint.

Here is my document in png format. I want to extract the 300$ and 500$ (the value).

<?xml version="1`.`0" encoding="ISO-8859-1"?>
<adi>
    <asset>
        <electronics item="Mobile" name="Nokia" value="300$" />
        <electronics item="Mobile" name="Sony" value="500$" />
    </asset>
</adi>

Another doubt is, are the two sets, the different representation of same xml ?

<?xml version="1.0 encoding="ISO-8859-1"?>
<adi>
    <asset>
        <electronics>
            <item> Mobile </item>
            <name>Nokia</name>
            <value>300$</value>
        </electronics>
        <electronics>
            <item> Mobile </item>
            <name>Sony</name>
            <value>500$</value>
        </electronics>
    </asset>
</adi>
like image 398
Parameswar Avatar asked Mar 19 '26 18:03

Parameswar


1 Answers

With regards to your second question, those two snippets do not represent the same XML content. Attributes and child elements are not equivalent. A child element can be the root element of some arbitrary XML tree, but attributes are atomic.

E.g., I could modify the second snippet like this:

<?xml version="1.0 encoding="ISO-8859-1"?>
<adi>
    <asset>
        <electronics>
            <item>
               Mobile
               <sub-item>Phone</sub-item>
            </item>
            <name>Nokia</name>
            <value>300$</value>
        </electronics>
        <electronics>
            <item> Mobile </item>
            <name>Sony</name>
            <value>500$</value>
        </electronics>
    </asset>
</adi>

where I have added <sub-item>Phone</sub-item> to the first <item> element. However, there's no equivalent if item is an attribute instead, as in the first snippet.

like image 118
Danwizard208 Avatar answered Mar 24 '26 12:03

Danwizard208



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!