Access specific data in a complex XML file

edited May 2015 in How To...

Hello, I'm loading XML data from an API and this is a simplified versione of the structure of the XML:

I need to access the parameter "name" inside CreditArtist. I tried with all the combination of getChildren and getChild (following the examples in the reference) but I couldn't wrap my head around this and couldn't access that data.

<Credits xmlns="*********" xmlns:i=""> <status>ok</status> <code>200</code> <parameters> <apiKey>*********</apiKey> <id>*********</id> <format>xml</format> </parameters> <view> <total>*********</total> </view> <credits> <NMCredit> <id>*********</id> <title>*********</title> <primaryartists> <CreditArtist> <id>*********</id> <name>*********</name> </CreditArtist> </primaryartists> <year>*********</year> <credit>*********</credit> <type>*********</type> </NMCredit> <NMCredit> <id>*********</id> <title>*********</title> <primaryartists> <CreditArtist> <id>*********</id> <name>*********</name> </CreditArtist> </primaryartists> <year>*********</year> <credit>*********</credit> <type>*********</type> </NMCredit> </credits> </Credits>


  • edited May 2015 Answer ✓

    Disclaimer: 1st time getting a longer glance at XML! ~:>

    I tried with all the combination of getChildren() and getChild().

    AFAIK, getChild("") returns the 1st found element w/ that name.
    Therefore, we should only use it when there's only 1 child w/ that name.

    For example: There's only 1 child called "credits" in your ".xml" sample.
    So we can use getChild("credits") in place of getChildren("credits"). :-\"

    Logically otherwise, we need to rely on getChildren(""), which returns an array of XML[].

    For example: There are 2 children called "NMCredit" in your ".xml" sample.
    Therefore we need to use getChildren("NMCredit"). >-)

    I need to access the parameter "name" inside "CreditArtist".

    Actually, "name" is also a child element just like "CreditArtist" and others are.
    What you want is the content of each existing element "name", right?
    So you need to use getContent() on it:

    However, there's a long road ahead before reaching last child element "name"! #:-S
    The sample below is my attempt on it.
    Tell me whether it's worked for ya and you have understood how it works: O:-)

    // 2015-May-13
    XML xml;
    String[] names;
    xml = loadXML("credits.xml");
    XML[] NMCredit = xml.getChild("credits").getChildren("NMCredit");
    names = new String[NMCredit.length];
    int idx = 0;
    for (XML element : NMCredit)  names[idx++] = element
  • For proper formatting of code, you must not hit C then paste code.
    You must paste the code, select it, then hit the C button.

  • @GoToLoop, thanks! it solved my problem! I can read all the data from the API, now I just need to understand how to change the api request by following the user input but I think this matter should have its own topic. :D

    @PhiLho: I will

Sign In or Register to comment.