Making constructor for XML in Processing, Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mamacach
    New Member
    • Apr 2014
    • 1

    Making constructor for XML in Processing, Java

    I am trying to visualize data from an Open Data web page. The data is parsed from an XML-file with geo coordinates (latitude and longitude).

    I am trying to make this XML-reader universal through OOP, so that I can use it for other purposes for another time. At the moment the XML-reader works, but I am having a bit of a hard time appropriating classes and objects in the reader. In my case I want to make one function that splits the lat/long coordinates and converts them to pixels. Then I also want a function that draws a polygon where the specific coordinates are.

    But before making an XML-reader class I have to know what constructor my XML-reader is going to have.

    So my question: Is there a standard (and maybe also a simple) way to do this in Java?

    My code looks like this:

    Code:
    XML xml; 
    ArrayList coordinates; 
    
    void setup() {
    
      coordinates=new ArrayList(); 
    
      xml = loadXML("parkerudenbom.xml");
    
      XML[] folder = xml.getChildren("Folder");
    
      XML[] Placemark=folder[0].getChildren("Placemark"); 
    
      println(Placemark.length);
    
      for(int i = 0; i < Placemark.length; i++) {
        XML[] Polygon=Placemark[i].getChildren("Polygon"); 
        for(int j= 0; j < Polygon.length; j++) {  
    
          XML[] outerBoundaryIs=Polygon[j].getChildren("outerBoundaryIs"); 
          XML[] LinearRing=outerBoundaryIs[0].getChildren("LinearRing"); 
          XML[] coords = LinearRing[0].getChildren("coordinates"); 
    
          coordinates.add(coords[0].getContent()); 
    
          println(coords[0].getContent()); 
        }
    
      }
    
    }
    The xml file is this one: https://www.dropbox.com/s/3p7l2mqr7fcjjrn/parkerudenbom.x ml
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You can use Xpath to get your list of coordinates much more comfortable and with less code.
    Like
    Code:
    NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
    Just replace "expression " with the value "/Folder/Placemark", then loop through the nodeList with a foreach loop and do it again inside loop using expression "Polygon/outerBoundaryIs/LinearRing/coordinates". Or use "Polygon/outerBoundaryIs/LinearRing/coordinates/coordinates[1]" to select the first coordinate if there are more than one.

    Comment

    Working...