We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am working with some XML data, and I stumbled upon some rather interesting behavior.
Sketch:
XML xml = loadXML("xml.xml");
printArray(xml.getChildren());
println("-----");
XML[] children = xml.getChildren();
for (int i = 0; i < children.length; i++) {
println(children[i] + ":");
XML[] rows = children[i].getChildren();
for (int r = 0; r < rows.length; r++) {
println(" row " + r + " '" + rows[r] + "'" +":");
XML[] cols = rows[r].getChildren();
for (int c = 0; c < cols.length; c++) {
println(" col " + c + " '" + cols[c] + "'");
}
}
}
xml.xml:
<?xml version="1.0"?>
<node>
<table>
<row>
<col>1,1</col>
<col>1,2</col>
</row>
<row>
<col>2,1</col>
<col>2,2</col>
</row>
<row>
<col>3,1</col>
<col>3,2</col>
</row>
</table>
</node>
Output:
[0]
[1] <table><row><col>1,1</col><col>1,2</col></row><row><col>2,1</col><col>2,2</col></row><row><col>3,1</col><col>3,2</col></row></table>
[2]
-----
:
<table><row><col>1,1</col><col>1,2</col></row><row><col>2,1</col><col>2,2</col></row><row><col>3,1</col><col>3,2</col></row></table>:
row 0 '':
row 1 '<row><col>1,1</col><col>1,2</col></row>':
col 0 ''
col 1 '<col>1,1</col>'
col 2 ''
col 3 '<col>1,2</col>'
col 4 ''
row 2 '':
row 3 '<row><col>2,1</col><col>2,2</col></row>':
col 0 ''
col 1 '<col>2,1</col>'
col 2 ''
col 3 '<col>2,2</col>'
col 4 ''
row 4 '':
row 5 '<row><col>3,1</col><col>3,2</col></row>':
col 0 ''
col 1 '<col>3,1</col>'
col 2 ''
col 3 '<col>3,2</col>'
col 4 ''
row 6 '':
:
As you can see, this yields some strange results, where every even array index is empty. This happened to me when I was drawing such a table using index numbers, and totally broke my sketch. What could be causing this?
Answers
https://Processing.org/reference/XML_getContent_.html
https://Forum.Processing.org/two/discussion/17692/how-to-read-deeper-into-xml#Item_5
@GoToLoop that doesn't help very much, as in my actual program I need to read the children of
table
, which have various names, not onlyrow
(those names arerow
andtitle
).Also I don't understand why did you direct me to
XML#getContent()
, I have no idea how to use it to my advantage. The forum link doesn't help aswell, because it's been made to read elements with only one name (forecastday
).I dunno the reason why there are some XML from getChildren() which yields an empty String.
Perhaps they're some internal stuff not supposed to be printed.
That's why you should focus on getting the content w/ getContent() rather than attempting to print each single XML object.
Well, maybe I'll just have
x
andy
values that will only get incremented when the XML is not empty, that should help. Anyways, thanks for the effort.