Loading in points from XML

edited February 2016 in Questions about Code

I'm doing a little text to load in some points from an xml file and loading them into an arrayList to display them on screen as ellipses. I've got some code now but when I run it I only get one ellipse drawn at 0,0 even though that isn't one of the points that I'm trying to load in.

Here is my code with my class Points

class Points {
  float xPos, yPos;

  Points(float _xPos, float _yPos) {

    xPos = _xPos;
    yPos = _yPos;
  }
}

XML xml;
ArrayList<Points> point = new ArrayList<Points>();

void setup() {
  size(640, 360);
  background(255);
  //point = new Points(-10, -10);
  //loadData();

  xml = loadXML("newPointList.xml");
  XML[] children = xml.getChildren("points");

  //point = new Points[children.length];

  for (int i = 0; i < children.length; i ++) {

    XML xVal = children[i].getChild("x");
    float x = xVal.getInt ("x");

    XML yVal = children[i].getChild("y");
    float y = xVal.getInt ("y");

    Points addingData = new Points(x, y);
    println("x " + x + ", " + "y " + y);
    point.add(addingData);
  }
}

void draw() {

  for (int i = 0; i< point.size(); i++) {

    Points drawPoints = point.get(i);
    ellipse(drawPoints.xPos, drawPoints.yPos, 10, 10);
  }
}

Here is the basic XML of points I'm trying to use called newPointList.xml

<?xml version="1.0"?>
<pointList>
<points>
<x>30</x>
<y>20</y>
</points>
<points>
<x>85</x>
<y>20</y>
</points>
<points>
<x>85</x>
<y>75</y>
</points>
</pointList>

Feel like the process is right, but still only has a point as zero. Any help would be appreciated. Thanks

Answers

  • edited February 2016

    Removed by SiriusCG.

  • edited February 2016
    // forum.Processing.org/two/discussion/14891/loading-in-points-from-xml
    // GoToLoop (2016-Feb-13)
    
    size(150, 100);
    smooth(4);
    noLoop();
    
    ellipseMode(CENTER);
    strokeWeight(2.5);
    stroke(0);
    fill(#FFFF00);
    background(0350);
    
    final XML xml = loadXML("newPointList.xml");
    final XML[] points = xml.getChildren("points");
    final PVector[] dots = new PVector[points.length];
    
    printArray(points);
    
    for (int i = 0; i < dots.length; ++i) {
      final XML point = points[i];
      final int x = point.getChild("x").getIntContent();
      final int y = point.getChild("y").getIntContent();
      dots[i] = new PVector(x, y);
    }
    
    println();
    printArray(dots);
    
    for (final PVector v : dots)  ellipse(v.x, v.y, 10, 10);
    
  • Thanks for your response GoToLoop. Nice implementation.

    I wanted to use an ArrayList so I could remove elements from the array at a later stage. I've tried to implement this previously and with your code but I'm not sure how to create an arrayList of type XML and PVector[] and then use that in a similar way to load in the points into the arrayList.

    Be grateful to know how that would be done.

    Thanks

  • edited February 2016 Answer ✓

    https://Processing.org/reference/ArrayList.html

    // forum.Processing.org/two/discussion/14891/loading-in-points-from-xml
    // GoToLoop (2016-Feb-13)
    
    import java.util.List;
    
    size(150, 100);
    smooth(4);
    noLoop();
    
    ellipseMode(CENTER);
    strokeWeight(2.5);
    stroke(0);
    fill(#FFFF00);
    background(0350);
    
    final XML xml = loadXML("newPointList.xml");
    final XML[] points = xml.getChildren("points");
    final List<PVector> dots = new ArrayList<PVector>(points.length);
    
    printArray(points);
    
    for (final XML point : points) {
      final int x = point.getChild("x").getIntContent();
      final int y = point.getChild("y").getIntContent();
      dots.add(new PVector(x, y));
    }
    
    println();
    print(dots);
    
    for (final PVector v : dots)  ellipse(v.x, v.y, 10, 10);
    
  • Thanks for this. Makes sense.

Sign In or Register to comment.