How to import data from an XML-file into Processing

edited April 2014 in How To...

Hello,

I'm currently working on a school project where I need to visualize different areas of a city in Processing. I have a KML-file that I've converted to XML, that contains coordinates of different green areas that I want to insert in my visualisation, but I'm not really sure how to import the data. I've inserted the XML-file into a XML Viewer to get an overview, and the data that I want to import is in the folder for all of the 57 green areas, all named "outerBondaryIs" ( https://www.dropbox.com/s/ysx0fr3anjrgy2z/XML Overview.png ).

So far, I've tried to use the example from the processing-homepage ( http://www.processing.org/reference/XML.html ), but I can't figure out how to use my own XML-file.

Here's the XML-file that I want to use: https://www.dropbox.com/s/4gb45pitft259dl/parker.xml

My teacher wants us to use classes in our code so the xmlreader has to be in a class as well. By the way, I'm very new to Processing so I'm seeking a simple solution ( or a thorough explaination on how to do it).

I hope that someone wants to help!

Thanks in advance,

M

Tagged:

Answers

  • Thank you for your reply. I would like to keep it in XML - and if it is possible I would like to do it without using a library. Any other suggestions? M

  • here....

    since you want to collect area data I propose a class area

    since we don't know how many you have I also propose a ArrayList of that class

    you would have to do the tutorial on OOP

    also read the reference on ArrayList please

    the idea is to loop over all Placemarks in the xml put each with the name into a object (type Area) and put this into the ArrayList

    To Do

    you have to finish the class (constr and draw)

    you have to make the object correct in the loop

    there might be flaws with the parsing of the xml

    also I didn't have a clue as to how get the geo-coors on the screen so I just killed the first 3 letters ;-)

    // The following short XML file is parsed 
    // in the code below. It must be in the project's "data" folder.
    //
    
    
    
    XML xml;
    
    int bluePart = 10; 
    
    ArrayList<Area> myAreas = new ArrayList(); 
    
    
    void setup() {
    
      size (700, 700); 
    
      xml = loadXML("parker.xml");
    
      // println (xml);
    
    
      println ("-------------------");
    
      XML[] children = xml.getChildren("Folder");
    
      //  println (children);
    
      println ("-------------------");
      println ("-------------------");
    
      // XML[] xml4 = xml.getChildren("Folder");
    
      // get all placemarks
    
      // ===
      // XML[] children = xml3.getChildren("Placemark");
      //
      println ( children.length );
    
      for (int i = 0; i < children.length; i++) {
        // int id = children[i].getInt("id");
        String coloring = children[i].getString("species");
        //String name = children[i].getContent();
        XML[] childrenPM = children[i].getChildren("Placemark");
    
        println ( childrenPM.length );
    
        // loop through all Placemarks
        for (int j = 0; j < childrenPM.length; j++) {
    
          XML[] nameHelper = childrenPM[j].getChildren("description");
          String name = getDataFromDesct(nameHelper); 
          println (name); 
    
          XML[] childrenPolygon = childrenPM[j].getChildren("Polygon");
          println ( childrenPolygon.length );
    
          // some don't have polygons !!!!!!!!!!
          if (childrenPolygon.length==1) {
            println ( "polgon #" );
    
            //XML[] childrenCoordinates = childrenPM[j].getChildren("coordinates");
            //println ( "childrenCoordinates length "+childrenCoordinates.length );
    
            String n1 = childrenPM[0].getContent();
            //println (n1);
            String [] listCoor = split (n1, "\n");
            // these are the coords for one area
    
            PVector[] arrayPVector = new PVector[listCoor.length] ; 
    
            for (int k = 0; k < listCoor.length; k++) {
              n1 = listCoor[k];
              // println (n1);
              String [] n2 = split (n1, ",");
              if (n2.length>1) {
                //
                n2[0] = trim(n2[0]);
                n2[1] = trim(n2[1]);
                //
                n2[0] = (n2[0].substring(3));
                n2[1] = (n2[1].substring(3));
    
                PVector coor1 = new PVector  ( int (trim(n2[0])), int(trim(n2[1] )));
                arrayPVector = (PVector[] ) append(arrayPVector, coor1); 
    
                //
                // point ( int (trim(n2[0])), int(trim(n2[1] )));
                fill(2, 2, bluePart);
                ellipse ( 30+ int (trim(n2[0]))/1000 * 4, 30+ int(trim(n2[1]))/1000 * 4, 5, 5);
    
                //  println ("point");
                //  println (int (trim(n2[0])) + " - " + int (n2[1])  + " - " + int (n2[2])   );
              } // if
            } // for
    
            // Area currentArea = new Area ( name, color(2, 2, bluePart), arrayPVector ); 
            // myAreas.add(currentArea);
    
            bluePart+=40;
            println (bluePart+" _________________");
          } // if
        } // for
    
        //
        //   println(name);
        println("---------------");
      } // for 
    
      //
    } // func 
    
    void draw() {
      //
    }
    
    String getDataFromDesct(XML[] nameHelper) {
    
      // String name2 = nameHelper.toString();
      String name2 = nameHelper[0].toString();
    
      // println (name2);
      String searchString1 = "<td>Navn</td><td>";
      int lenOfsearchString1 = searchString1.length();
      int pos1 = name2.indexOf(searchString1);
      if (pos1>1) {
        int pos2 = name2.indexOf("</td>", pos1+lenOfsearchString1+1);
        String name1  = name2.substring(pos1+lenOfsearchString1, pos2);
        return name1;
      }
      else 
        return "";
    } // func 
    
    //
    class Area {
    
      String name ; 
      PVector [] coordinates ; 
      color colorArea = color (random (255), random (255), random (255));
    
      // missing constr
    
      void draw () {
        //
      }
    } // class 
    //
    
  • you really have to do some house cleaning there

    I am not proud on this...

This discussion has been closed.