473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. XML xml; 
  3. ArrayList coordinates; 
  4.  
  5. void setup() {
  6.  
  7.   coordinates=new ArrayList(); 
  8.  
  9.   xml = loadXML("parkerudenbom.xml");
  10.  
  11.   XML[] folder = xml.getChildren("Folder");
  12.  
  13.   XML[] Placemark=folder[0].getChildren("Placemark"); 
  14.  
  15.   println(Placemark.length);
  16.  
  17.   for(int i = 0; i < Placemark.length; i++) {
  18.     XML[] Polygon=Placemark[i].getChildren("Polygon"); 
  19.     for(int j= 0; j < Polygon.length; j++) {  
  20.  
  21.       XML[] outerBoundaryIs=Polygon[j].getChildren("outerBoundaryIs"); 
  22.       XML[] LinearRing=outerBoundaryIs[0].getChildren("LinearRing"); 
  23.       XML[] coords = LinearRing[0].getChildren("coordinates"); 
  24.  
  25.       coordinates.add(coords[0].getContent()); 
  26.  
  27.       println(coords[0].getContent()); 
  28.     }
  29.  
  30.   }
  31.  
  32.  
  33.  
The xml file is this one: https://www.dropbox.com/s/3p7l2mqr7fcjjrn/parkerudenbom.xml
Apr 29 '14 #1
1 1394
chaarmann
785 Expert 512MB
You can use Xpath to get your list of coordinates much more comfortable and with less code.
Like
Expand|Select|Wrap|Line Numbers
  1. NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
  2.  
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.
Apr 29 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
3
by: Kevin C | last post by:
Will object instantiation of an object not occur until the static constructor has run? In other words, if I have logic in a static constructor that takes, lets say 5 seconds. Will all other...
18
by: Peter Gummer | last post by:
This is a design question. I have a project containing a dozen or so classes with protected internal constructors. My reason for having protected internal constructors is to prevent classes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.