Mitra
Mitra

Reputation: 31

How to read tag attribute of XML in Inno Setup

Here is the XML file :

<book_info>
    <book title = "Peter Pan">
        <publisher name="Penguin" edition="2nd" date ="2/6/2016" />
        <Page size= "207" />
        <author name = "J M Barrie"/>
        <info genre  = "novel" lang = "English"/>
    </book>
    <book title = "Room of Many Color">
        <publisher name="Penguin" edition="1st" date ="3/11/2000" />
        <Page size= "387" />
        <author name = "Ruskin Bond"/>
        <info genre  = "sort stories" lang = "English"/>
    </book>
</book_info>

Here is how I am trying to access the publisher name :

bookXML := CreateOleObject('Msxml2.DOMDocument.6.0');
bookXML.async := False;
bookXML.load(ExpandConstant('{tmp}\book.xml'));
bookNode := bookXML.SelectSingleNode('//book_info/book[name/text()=' + bookName + ']');
bookPubName := bookNode.SelectSingleNode('publisher[name]').text;

But it is always giving NIL interface exception for the last line.

bookPubName := bookNode.SelectSingleNode('publisher[name]').text;

I checked How to select XML tag based on value of its child tag in Inno Setup but could not resolve.

Upvotes: 2

Views: 702

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202494

It's not really clear what you want to do. Your XPath for bookNode refers to a name tag, but a book has no name tag, not even an name attribute.


Anyway, let's assume that you want to find out a value of name attribute of publisher tag of a book with title (?) attribute equal to a given value.

  • Refer to title attribute using @title instead of referring to a text content of a non-existing name tag:

    //book_info/book[@name=...]
    
  • Wrap the string value to single-quotes (you have to double them in Pascal string):

    '//book_info/book[@name=''' + bookName + ''']'
    
  • To select a value of name attribute of publisher tag, again use @name syntax (name refers to a non-existing sub-tag).

    publisher[@name]
    

And you can merge both XPaths into one like:

bookPubName :=
    bookXML.SelectSingleNode(
        '//book_info/book[@title=''' + bookName + ''']/publisher/@name').text;

Upvotes: 1

Related Questions