name
XMLElement
/*
this is a more complex example demonstrating the possibilities
of proxml. In the example the user can draw ellipses that are saved
into a xml file. This file is loaded on the next start of the program.
If there exists an xml file it is loaded and the
content is drawn as ellipses. Otherwise the xml element is created.
*/

import proxml.*;

//to store the background after painting an ellipse
PImage back;

//xml element to store and load the drawn ellipses
XMLElement ellipses;
XMLInOut xmlInOut;

int xPos = 0;
int yPos = 0;

void setup(){
  size(400,400);
  smooth();
  background(255);

  //load ellipses from file if it exists
  xmlInOut = new XMLInOut(this);
  try{
    xmlInOut.loadElement("ellipse.xml"); 
  }catch(Exception e){
    //if the xml file could not be loaded it has to be created
    xmlEvent(new XMLElement("ellipses"));
  }
}

void xmlEvent(XMLElement element){
  ellipses = element;
  initEllipses();
    //initialise PImage for background
  back = new PImage(width,height);
  loadPixels();
  back.pixels = pixels;
}

void draw(){
}

//draw all ellipses saved in the xml file
void initEllipses(){
  ellipses.printElementTree(" ");
  XMLElement ellipse;
  XMLElement position;
  XMLElement size;
  
  for(int i = 0; i < ellipses.countChildren();i++){
    ellipse = ellipses.getChild(i);
    position = ellipse.getChild(0);
    size = ellipse.getChild(1);
    ellipse(
      position.getIntAttribute("xPos"),
      position.getIntAttribute("yPos"),
      size.getFloatAttribute("Xsize"),
      size.getFloatAttribute("Ysize")
    );
  }
}

void mousePressed(){
  xPos = mouseX;
  yPos = mouseY;
}

void mouseDragged(){
  background(back);
  ellipse(xPos,yPos,abs(xPos-mouseX),abs(yPos-mouseY));
}

void mouseReleased(){
  XMLElement ellipse = new XMLElement("ellipse");
  ellipses.addChild(ellipse);
  XMLElement position = new XMLElement("position");
  position.addAttribute("xPos",xPos);
  position.addAttribute("yPos",yPos);
  ellipse.addChild(position);
  XMLElement size = new XMLElement("size");
  size.addAttribute("Xsize",abs(xPos-mouseX));
  size.addAttribute("Ysize",abs(yPos-mouseY));
  ellipse.addChild(size);
  xmlInOut.saveElement(ellipses,"ellipse.xml");
  loadPixels();
  back.pixels = pixels;
}
description
XMLElement is the basic class of proXML. You can build a XMLElement and add Attributes and children, or load it from a certain path using XMLInOut. Text is also handled as XMLElement, so if you want to get the text of an element you have to call element.firstChild().getText(). If you have a part where xml nodes are inside text they seperate the text into several elements, so for example "this is <bold>bold</bold> .." would result in the following element list: "this is ",<bold>,"bold",</bold>," .."
constructors
XMLElement(name, pcdata);
XMLElement(name, attributes, children);
XMLElement(name);
XMLElement(name, children);
XMLElement(name, attributes);
parameters
name
String, name of the element
pcdata
boolean, true if the element is a pcdata section
attributes
Hashtable, attributes for the element, with names and values
children
Vector, the children of the element
children
Vector, children of the element
attributes
Hashtable, attributes of the element, with names and values
methods
With addAttribute() you can add attributes to a XMLElement.
Adds the specified node to the XML element's child list.
This method returns the number of all nodes of a XMLElement.
Use this method to count the attributes of a XMLElement.
With countChildren() you get the number of children of a XMLElement.
Returns the first child of the element.
Returns the value of a given attribute.
Returns a String Array with all attribute names of an Element.
Use getChild() to get a certain child element of a XMLElement.
Returns an Array with all the children of an element.
Use getDepth to get the maximum depth of an Element to one of its leaves.
Use this method to get the name or text of an XMLElement.
Use getFloatAttribute() to get the value of an attribute as float value.
Use getIntAttribute() to get the value of an attribute as int value.
Returns the name of the element.
With getParent() you can get the parent of a XMLElement.
Returns the text of the element.
This method checks if the XMLElement has the given Attribute.
Use this method to check if the XMLElement has attributes.
Specifies whether or not the XML object has child nodes.
Checks if a XMLElement is a text element
Returns the last child of the element.
Returns the next sibling of the element.
Returns the previous sibling of the element.
Prints out the XML content of the element.
Removes the specified XML element from its parent.
Use toString to get the String representation of a XMLElement.
usage
Web & Application
related