We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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="http://www.w3.org/2001/XMLSchema-instance">
<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>
Answers
Disclaimer: 1st time getting a longer glance at XML! ~:>
https://processing.org/reference/XML_getChild_.html
https://processing.org/reference/XML_getChildren_.html
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 ofgetChildren("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")
. >-)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: https://processing.org/reference/XML_getContent_.html
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:-)
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