Search code examples
xmlswiftparsingrootswxmlhash

SWXMLHash compact root element


I have a question I hope will be quick. I am parsing XML returned by eBay’s API using SWXMLHash for Swift. Some of the returns get quite verbose.

such as:

 xml["findCompletedItemsResponse"][“searchResults"][“item”]...[“sellerID"].element?.text

How would I go about compacting this to say:

xml[rootVar][“item”]...[“sellerID"].element?.text

is there a way to specify this in this instantiation? I have tried (which obviously did not work):

let xml = SWXMLHash.config { 
    config in
    config.shouldProcessLazily  = true
    config.shouldProcessNamespaces  = true
    rootElementName = "findCompletedItemsResponse"
}.parse(response);

Solution

  • I would suggest doing something like:

    let rootXml = xml["findCompletedItemsResponse"]["searchResults"]
    // ...
    let sellerID = rootXml["item"]...["sellerID"].element?.text
    

    Each indexed level can be saved off into a variable - it is just an instance of XMLIndexer that has been indexed down a certain level.

    Hope this helps!