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.
IndexProgramming Questions & HelpSyntax Questions › proXML - change and add new elements
Page Index Toggle Pages: 1
proXML - change and add new elements (Read 800 times)
proXML - change and add new elements
Jan 16th, 2010, 3:47pm
 
hello guys,
i've got one more problem using proxml.
I have a xml file saving 3 values for each element inside. Now when executing my Processing script I want to go through all of the xm-elements and change all those whose 3rd value is 0 instead of 1.

<?xml version="1.0" encoding="ISO-8859-1"?>
<positions>
<type>
  <dataType v1="200" v2="12" v3="1"/>
</type>
<type>
  <dataType v1="150" v2="12" v3="1"/>
</type>
<type>
  <dataType v1="100" v2="12" v3="0"/>
</type>
</positions>

So in my example above I have to write a script that changes in the third element the last value from 0 to 1.

I don't know how to go through the xml data and just concern about those rows in which the third value is 0 instead of 1. I then want to change it from 0 to 1 and save it.

Next time I execute the script I do the same thing. I only want to change the first element I can find in which there is a 0 instead of 1 at value "v3".

Can you guys give me a hint how to do so?
I tried to start it this way, maybe you can come up with some extensions on this code?
Thanks a lot!

Code:
  proxml.XMLElement[] allValues = element.getChildren();

for (int i = 0; i < allValues.length;i++)
{
proxml.XMLElement Value = allValues[i];
proxml.XMLElement param = Value.getChild(0);
//println(param.getName());
if (!param.getName().equals("dataType"))
continue; // Ignore unknown child
value1 = param.getIntAttribute ("v1");
value2 = param.getIntAttribute("v2");
value3 = param.getIntAttribute("v3");

if (value3 == 0) {
// i think i have to add a new attribute at this place?!

}

}

Re: proXML - change and add new elements
Reply #1 - Jan 17th, 2010, 8:42am
 
Have you tried this?:

param.addAttribute("v3", 1);
Re: proXML - change and add new elements
Reply #2 - Jan 17th, 2010, 11:40am
 
Ok i tried to just add your line of code but then nothing happens. so i tried to save it again, but then i get this as an error: I dont know why it doesnt work as i have the right path to the xml-file and i used this       xmlInOut.saveElement(DataXML,"data1.xml"); in other projects as well!?

java.lang.reflect.InvocationTargetException
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at proxml.XMLInOut$Loader.run(XMLInOut.java:457)
     at java.lang.Thread.run(Thread.java:637)
Caused by: java.lang.NullPointerException
     at proxmlTestInOut.xmlEvent(proxmlTestInOut.java:47)
     ... 6 more




Code:
import proxml.*;
import proxml.XMLElement;

XMLInOut xmlInOut;
boolean xmlCheck = false;
XMLElement DataXML;


void setup() {
xmlInOut = new XMLInOut(this);
try{
xmlInOut.loadElement("data1.xml");
}
catch(Exception e){
//if the xml file could not be loaded it has to be created
xmlEvent(new XMLElement("data"));

}
}

void draw() {


}

void xmlEvent(XMLElement element){

proxml.XMLElement[] allValues = element.getChildren();

for (int i = 0; i < allValues.length;i++)
{
proxml.XMLElement Value = allValues[i];
proxml.XMLElement param = Value.getChild(0);
//println(param.getName());
if (!param.getName().equals("dataType"))
continue; // Ignore unknown child
int value1 = param.getIntAttribute ("v1");
int value2 = param.getIntAttribute("v2");
int value3 = param.getIntAttribute("v3");

if (value3 == 0) {
param.addAttribute("v3", 1);
println ("done saving");
xmlInOut.saveElement(DataXML,"data1.xml");

}
}
}






Re: proXML - change and add new elements
Reply #3 - Jan 17th, 2010, 2:03pm
 
DataXML is never initialized, so it might be the cause for the NPE you report.
Beside, you save something in the file you are reading (before the end of the reading), so there might be a clash...

I don't how if you are aware of that, but AFAIK, Processing's XML code and proXML both read XML in SAX way, ie. generating an event of each XML element. They don't do it the DOM way, reading the whole file in memory and organizing it as a tree you can alter, then save.
DOM libraries actually rely (often) on SAX to build the tree. So you can do it in a similar way.
The XML you show seems quite linear (no deep tree), so you can read the XML file and put the found elements in an ArrayList, as objects modeling the data (apparently you can even use the simple PVector to store this data).
Once all the data is read, you can alter the values in the objects stored in the ArrayList.
And once done, you can save this list in XML form, by hand or with proXML (create element, set its attributes, save, rinse and repeat).
Page Index Toggle Pages: 1