We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
XML troubles (Read 3562 times)
XML troubles
Apr 28th, 2005, 1:06am
 
I'm working on parsing XML data into processing, and have enough things working with nanoxml, but have a small problem I need an answer for. My XML file will be in this format:


Code:

<?xml version="1.0" encoding="iso-8859-1"?>
<RECORD>
<chapter>augmented space</chapter>
<title>augmentation and monitoring</title>
<example>augmented space</example>
</RECORD>
<RECORD>
<chapter>metamedia</chapter>
<title>meta-data</title>
<example>Elements in Meta-Data</example>
</RECORD>


and in processing I can only parse what is between one <RECORD> tag (such as 'augmented space' 'augmentation and monitoring', etc. not its siblings ). I looked at the pages on XML and parsing, and can't figure out a way to do this. This is what my processing code looks like:

Code:

// Processing XML parsing example
// using nanoXML library by cyberElf (http://web.wanadoo.be/cyberelf/nanoxml/)
// author: info at toxi.co.uk
//
// for questions, please consult NanoXML javadocs

Object[] chapter;
XMLElement xml = new XMLElement();


void setup() {
size(400,300);
textFont(loadFont("Meta-Bold.vlw.gz"),24);
// load & parse XML
xml = new XMLElement();
xml2 = new XMLElement();
String xmlString="";
String[] lines=loadStrings("levTest.xml");
for(int i=0; i<lines.length; i++) xmlString+=lines[i];
xml.parseString(xmlString);

// retrieve all childnodes and store them in an array
chapter=xml.getChildren().toArray();
}

void draw() {
int x=10,y=30;
background(0xe0);
fill(0);
textSize(20);
noStroke();
for(int i=0; i<chapter.length; i++) {
XMLElement node=(XMLElement)chapter[i];
text(node.getContent(),x+5,y-6);
y+=50;
}
}


Does anyone have any tips or advice on how to parse for siblings of something like <RECORD>?

best,

Tim Jaeger
Re: XML troubles
Reply #1 - Apr 28th, 2005, 2:28am
 
that's because by definition a xml file is only allowed to have _1_ node at top-level (besides the <?xml ..> declaration (and comments). look it up at w3c .. so nanoxml might (should) ignore the second tag.

you could just put you <RECORD>s into one <all_records> tag.

btw: it is recommended to use lowercase tags ... but i think that's not (yet) a must.

F

it's here:
W3C: Extensible Markup Language (XML) 1.0
...
2.1 Well-Formed XML Documents
...
(2) There is exactly one element, called the root, or document element, no part of which appears in the content of any other element.
...
Re: XML troubles
Reply #2 - May 4th, 2005, 12:19am
 
I tried this method, but nothing was drawn to the screen (no text)...The XML code looks like this:

Code:

<?xml version="1.0" encoding="iso-8859-1"?>
<all_records>
<RECORD>
<chapter>augmented space</chapter>
<title>augmentation and monitoring</title>
<example>augmented space</example>
<chapter>metamedia</chapter>
<title>meta-data</title>
<example>Elements in Meta-Data</example>
</RECORD>
<RECORD>
<chapter>metamedia</chapter>
<title>meta-data</title>
<example>Elements in Meta-Data</example>
</RECORD>
</all_records>
     

When I run this following in my for() loop:

println(node.getContent());

both <RECORDS> (And everything inside them) is printed at the bottom of the screen..so everything from the XML file is being parsed into an array. Is this something to do with the "get.Content()" function?

best,
Tim
Re: XML troubles
Reply #3 - May 4th, 2005, 12:41am
 
One note is that if I do two println()s in the draw() method:

Code:

println(chapter.length);
println(node.getChildren());


The following is returned:

Code:

2

[<chapter>augmented space</chapter>, <title>augmentation and monitoring</title>, <example>augmented space</example>]

[<chapter>metamedia</chapter>, <title>meta-data</title>, <example>Elements in Meta-Data</example>]


So it is accurately saying that there are two child nodes, but if I do:

Code:

text(node.getChildren(),x,y);


An error message is returned stating :

Code:

Perhaps you wanted the overloaded version "void text(char $1, float $2, float $3);" instead?


I'm not quite sure what this means, but I take it I can't print the child nodes.
Re: XML troubles
Reply #4 - May 4th, 2005, 1:09am
 
if you are refering to the code given above, then it's clear: you are trying to get the contents of <RECORD> which has no text ... change the code to loop thru the children of <RECORD>, something like:
Code:

import nanoxml.*;
XMLElement xml;

void setup()
{
size( 200,200 );
noLoop();

XMLElement xml = new XMLElement();
Reader reader = new InputStreamReader(openStream("test.xml"));
try{
xml.parseFromReader(reader);
}
catch (Exception e) { e.printStackTrace(); }

Object[] records = xml.getChildren().toArray();

for ( int i=0; i<records.length; i++)
{
println( ((XMLElement)records[i]).getName() + i + ":" );
Object[] rec_info = ((XMLElement)records[i]).getChildren().toArray();
for ( int r=0; r<rec_info.length; r++ )
{
println( ((XMLElement)rec_info[r]).getName() + " > " + ((XMLElement)rec_info[r]).getContent() );
}
}
}


F
Re: XML troubles
Reply #5 - May 4th, 2005, 1:43am
 
thanks Fjen...works great..looks like I have to beef up on my java, though! I'll post the finished work when I have it finished Wink
Page Index Toggle Pages: 1