VTD-XML reading gives no results - java

I am trying to read a RSS content using VTD-XML. Below is a sample of RSS.
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<?xml-stylesheet type="text/xsl" href="rss.xsl"?>
<channel>
<title>MyRSS</title>
<atom:link href="http://www.example.com/rss.php" rel="self" type="application/rss+xml" />
<link>http://www.example.com/rss.php</link>
<description>MyRSS</description>
<language>en-us</language>
<pubDate>Tue, 22 May 2018 13:15:15 +0530</pubDate>
<item>
<title>Title 1</title>
<pubDate>Tue, 22 May 2018 13:14:40 +0530</pubDate>
<link>http://www.example.com/news.php?nid=47610</link>
<guid>http://www.example.com/news.php?nid=47610</guid>
<description>bla bla bla</description>
</item>
</channel>
</rss>
Anyway as you know, some RSS feeds can contain more styling info etc. However in every RSS, the <channel> and <item> will be common, at least for the ones I need to use.
I tried VTD XML to read this as quickly as possible. Below is the code.
VTDGen vg = new VTDGen();
if (vg.parseHttpUrl(appDataBean.getUrl(), true)) {
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/channel/item");
int result = -1;
while ((result = ap.evalXPath()) != -1) {
if (vn.matchElement("item")) {
do {
//do something with the contnets in the item node
Log.d("VTD", vn.toString(vn.getText()));
} while (vn.toElement(VTDNav.NEXT_SIBLING));
}
}
}
Unfortunately this did not print anything. What am I doing wrong here? Also non of the RSS feeds are very big, so I need to read them in couple of miliseconds. This code is on Android.

Related

Parsing the XML file having some unknown characters at starting

I am trying to parse an xml file which is having some unknown characters at starting and i am getting the error
isjava.lang.IllegalStateException: No successful match so far
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<!--RSS generated by RSSviaXmlTextWriter at Thu, 02 Mar 2017 16:35:42 GMT-->
<rss version="2.0">
<channel>
<title>The Tribune</title>
<link>http://www.tribuneindia.com/</link>
<description>Tribune News Service</description>
<item>
<category>Jammu & Kashmir</category>
<link>http://www.tribuneindia.com/news/jammu-kashmir/valley-schools-reopen-after-8-months/371227.html</link>
<title>Valley schools reopen after 8 months</title>
<image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_232657586.jpg</image>
<description>SRINAGAR: The schools in the Valley reopened fully today after remaining closed for eight months bringing back the liveliness that had been missing in the winter months. The schools were shut after the eruption of unrest following the killing of Hizbul commander Burhan Wani on July 8 last year.</description>
<pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate>
<guid>http://www.tribuneindia.com/news/jammu-kashmir/valley-schools-reopen-after-8-months/371227.html</guid>
</item>
<item>
<category>Jammu & Kashmir</category>
<link>http://www.tribuneindia.com/news/jammu-kashmir/registration-of-pilgrims-for-amarnath-yatra-begins/371228.html</link>
<title>Registration of pilgrims for Amarnath yatra begins</title>
<image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_232805408.jpg</image>
<description>JAMMU: Amid the chants of “Bham Bham Bhole”, the registration of pilgrims for this year’s pilgrimage to the Amarnath cave shrine commenced for both Baltal and Chandanwari routes here today.</description>
<pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate>
<guid>http://www.tribuneindia.com/news/jammu-kashmir/registration-of-pilgrims-for-amarnath-yatra-begins/371228.html</guid>
</item>
<item>
<category>Jammu & Kashmir</category>
<link>http://www.tribuneindia.com/news/jammu-kashmir/ladakh-worried-over-costly-air-travel-ahead-of-tourist-season/371235.html</link>
<title>Ladakh worried over costly air travel ahead of tourist season</title>
<image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_233238395.jpg</image>
<description>JAMMU: With Ladakh bracing up to host domestic and foreign tourists, the exorbitant air travel to the arid region continues to be a cause for worry for all stakeholders as the Civil Aviation Ministry is yet to make a formal commitment on “rationalisation of airfares” for Ladakh during peak tourist season from May to September.</description>
<pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate>
<guid>http://www.tribuneindia.com/news/jammu-kashmir/ladakh-worried-over-costly-air-travel-ahead-of-tourist-season/371235.html</guid>
</item>
<item>
<category>Jammu & Kashmir</category>
<link>http://www.tribuneindia.com/news/jammu-kashmir/take-steps-for-benefits-of-pilgrims-governor-tells-shrine-board-ceo/371220.html</link>
<title>Take steps for benefits of pilgrims, Governor tells Shrine Board CEO</title>
<image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_232356489.jpg</image>
<description>JAMMU: Governor NN Vohra today said several important issues related to Katra and its surrounding areas were conclusively addressed in the meeting held at Raj Bhawan on February 17 in which Chief Minister Mehbooba Mufti was also present.</description>
<pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate>
<guid>http://www.tribuneindia.com/news/jammu-kashmir/take-steps-for-benefits-of-pilgrims-governor-tells-shrine-board-ceo/371220.html</guid>
</item>
<item>
<category>Jammu & Kashmir</category>
<link>http://www.tribuneindia.com/news/jammu-kashmir/police-find-weapons-in-cross-loc-truck/371225.html</link>
<title>Police find weapons in cross-LoC truck</title>
<image>http://images.tribuneindia.com/cms/gall_content/archive/</image>
<description>SRINAGAR: A cache of weapons, which was being smuggled for militants in the Kashmir valley, was recovered from a truck engaged in cross-LoC trade in north Kashmir’s Baramulla district, the police said today.</description>
<pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate>
<guid>http://www.tribuneindia.com/news/jammu-kashmir/police-find-weapons-in-cross-loc-truck/371225.html</guid>
</item>
<item>
<category>Jammu & Kashmir</category>
<link>http://www.tribuneindia.com/news/jammu-kashmir/shutdown-in-bannihal-town-over-twin-deaths/371579.html</link>
<title>Shutdown in Bannihal town over twin deaths</title>
<image>http://images.tribuneindia.com/cms/gall_content/archive/</image>
<description>JAMMU: A shutdown marred life in a Jammu and Kashmir town on Thursday amid allegations that the driver and a cleaner in a truck found dead in an accident had actually been murdered.</description>
<pubDate>Thu, 02 Mar 2017 13:28:31 GMT</pubDate>
<guid>http://www.tribuneindia.com/news/jammu-kashmir/shutdown-in-bannihal-town-over-twin-deaths/371579.html</guid>
</item>
</channel>
</rss>
I think the line number 2 creates the problem which is
<!--RSS generated by RSSviaXmlTextWriter at Thu, 02 Mar 2017 16:35:42 GMT-->
obviously the first line is treated as comment by the xmlpullparser but the issue is with the second line.
The Parser is i guess unable to parse the second line because it is searching for the starting tag and it does encounter the character .
Here is my parser
public class SitesXmlPullParserTribuneLocal{
static final String KEY_SITE = "item";
static final String KEY_NAME = "title";
static final String KEY_LINK = "link";
static final String KEY_ABOUT = "description";
static final String KEY_IMAGE_URL = "image";
static final String KEY_DATE = "pubDate";
private static boolean firstCheck = true;
public static List<NewsItems> getStackSitesFromFile(Context ctx) {
// List of StackSites that we will return
List<NewsItems> newsItems;
newsItems = new ArrayList<NewsItems>();
// temp holder for current StackSite while parsing
NewsItems curNewsItems = null;
// Temporary Holder for current text value while parsing
String curText = "";
try {
// Get our factory and PullParser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
// Open up InputStream and Reader of our file.
FileInputStream fis = ctx.openFileInput("TribuneLocal.xml");
// Log.e("ERROR at TribuneLocal", String.valueOf(ctx.openFileInput("TribuneLocal.xml")));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
// point the parser to our file.
xpp.setInput(reader);
// get initial eventType
int eventType = xpp.getEventType();
Log.e("TagName Local", String.valueOf(eventType));
//To get the actual location to start parsing from
boolean actual_work = false;
// Loop through pull events until we reach END_DOCUMENT
Log.e("You Reached To", "Mark1");
while (eventType != XmlPullParser.END_DOCUMENT) {
// Get the current tag
Log.e("You Reached To", "Mark2");
String tagName = xpp.getName();
Log.e("You Reached To", "Mark3");
// Log.e("TagName is", tagName);
// React to different event types appropriately
if (eventType != XmlPullParser.START_TAG ) {
Log.e("EventType Inside", String.valueOf(eventType));
// firstCheck = false;
eventType = xpp.next();
continue;
}
xpp.setFeature("http://xmlpull.org/v1/doc/features.html#relaxed", true);
Log.e("EventType Outside", String.valueOf(eventType));
switch (eventType) {
case XmlPullParser.START_TAG:
Log.e("You Reached To", "Mark4");
if (tagName.equalsIgnoreCase(KEY_SITE)) {
// If we are starting a new <news> block we need
//a new NewsItems object to represent it
actual_work = true;
curNewsItems = new NewsItems();
}
break;
case XmlPullParser.TEXT:
//grab the current text so we can use it in END_TAG event
curText = xpp.getText();
break;
case XmlPullParser.END_TAG:
if (tagName.equalsIgnoreCase(KEY_SITE) && actual_work) {
// if </item> then we are done with current Site
// add it to the list.
newsItems.add(curNewsItems);
} else if (tagName.equalsIgnoreCase(KEY_NAME) && actual_work) {
// if </title> use setTitle() on curSite
Log.e("TITLE IS ",curText);
curNewsItems.setTitle(curText);
} else if (tagName.equalsIgnoreCase(KEY_LINK) && actual_work) {
// if </link> use setLink() on curSite
Log.e("LINK IS ",curText);
curNewsItems.setLink(curText);
} else if (tagName.equalsIgnoreCase(KEY_ABOUT) && actual_work) {
// if </description> use setDescription() on curSite
Log.e("DESCRIPTION IS ",curText);
curNewsItems.setDescription(curText);
} else if (tagName.equalsIgnoreCase(KEY_DATE) && actual_work) {
// if </image> use setImgUrl() on curSite
Log.e("DATE IS : ",curText);
curNewsItems.setDate(curText);
}else if (tagName.equalsIgnoreCase(KEY_IMAGE_URL) && actual_work) {
// if </image> use setImgUrl() on curSite
Log.e("IMAGE IS : ",curText);
curNewsItems.setImgUrl(curText);
}
break;
default:
break;
}
//move on to next iteration
eventType = xpp.next();
}
} catch (Exception e) {
Log.e("Tribune Local File","There is an ERROR Parsing It");
}
// return the populated list.
return newsItems;
}
}
the xml file is ok
this parses the file, and then you can use the DOM and extract elements, ...
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// PARSE
Document document = builder.parse(new InputSource(new InputStreamReader(fis)));

SoapUI: response with CDATA is giving null. searched various article but no luck

Hello I have below SOAP reponse.
`
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetWeatherResponse xmlns="http://www.webserviceX.NET">
<GetWeatherResult><![CDATA[<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Cape Town, Cape Town International Airport, South Africa (FACT) 33-59S 018-36E 0M</Location>
<Time>Jun 04, 2016 - 05:00 AM EDT / 2016.06.04 0900 UTC</Time>
<Wind> from the SE (130 degrees) at 21 MPH (18 KT):0</Wind>
<Visibility> greater than 7 mile(s):0</Visibility>
<SkyConditions> mostly clear</SkyConditions>
<Temperature> 60 F (16 C)</Temperature>
<DewPoint> 44 F (7 C)</DewPoint>
<RelativeHumidity> 55%</RelativeHumidity>
<Pressure> 30.39 in. Hg (1029 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>]]></GetWeatherResult>
</GetWeatherResponse>
</soap:Body>
</soap:Envelope>"`
for the following request: http://www.webservicex.net/globalweather.asmx.
I want to read XML Data in above response using script. but it is giving null.
I have tried script as below
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(messageExchange.responseContent)
holder.namespaces["ns"] = "http://www.webserviceX.NET/" def
weatherinfo= holder.getNodeValue("//ns:GetWeatherResult/text()")
log.info weatherinfo
bit Instead of getting above reponse I am getting NULL. I have read the SOAPUI documentation on CDATA but its not working.
I got answer. It was just one extra slash in namespace that was giving me null.
Both the below scripts are working now.
> def respXmlHolder = new
> com.eviware.soapui.support.XmlHolder(messageExchange.getResponseContentAsXml())
> respXmlHolder.namespaces["ns1"] ="http://www.webserviceX.NET" def
> CDATAXml= respXmlHolder.getNodeValue("//ns1:GetWeatherResult/text()")
> log.info CDATAXml
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(messageExchange.responseContent)
holder.namespaces["ns"] = "http://www.webserviceX.NET"
def weatherinfo= holder.getNodeValue("//ns:GetWeatherResult/text()")
log.info weatherinfo

how to parse xml element node susing pig script?

I am using pig latin for a large XML dump. I am trying to get the value of the xml nodes like location and temp_c in pig latin. The file is like
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?>
<current_observation version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.weather.gov/view/current_observation.xsd">
<credit>NOAA's National Weather Service</credit>
<credit_URL>http://weather.gov/</credit_URL>
<image>
<url>http://weather.gov/images/xml_logo.gif</url>
<title>NOAA's National Weather Service</title>
<link>http://weather.gov</link>
</image>
<suggested_pickup>15 minutes after the hour</suggested_pickup>
<suggested_pickup_period>60</suggested_pickup_period>
<location>Unknown Station</location>
<station_id>51WH0</station_id>
<observation_time>Last Updated on Dec 23 2014, 11:00 pm LST</observation_time>
<observation_time_rfc822>Tue, 23 Dec 2014 23:00:00 +1000</observation_time_rfc822>
<temperature_string>71.4 F (21.9 C)</temperature_string>
<temp_f>71.4</temp_f>
<temp_c>21.9</temp_c>
<water_temp_f>75.9</water_temp_f>
<water_temp_c>24.4</water_temp_c>
<wind_string>North at 24.6 MPH (21.38 KT)</wind_string>
<wind_dir>North</wind_dir>
<wind_degrees>20</wind_degrees>
<wind_mph>24.6</wind_mph>
<wind_gust_mph>0.0</wind_gust_mph>
<wind_kt>21.38</wind_kt>
<pressure_string>1015.0 mb</pressure_string>
<pressure_mb>1015.0</pressure_mb>
<dewpoint_string>58.1 F (14.5 C)</dewpoint_string>
<dewpoint_f>58.1</dewpoint_f>
<dewpoint_c>14.5</dewpoint_c>
</current_observation>
May be it will help you, try this out.
REGISTER piggybank.jar
DEFINE XPath org.apache.pig.piggybank.evaluation.xml.XPath();
A = LOAD 'xmls/your_file.xml' using org.apache.pig.piggybank.storage.XMLLoader('current_observation') as (x:chararray);
B = FOREACH A GENERATE XPath(x, 'current_observation/location'), XPath(x, 'current_observation/temp_c');
dump B;
use this:
data = LOAD '/path/your_file.xml'
USING org.apache.pig.piggybank.storage.StreamingXMLLoader(
'current_observation',
'credit, credit_URL, image, suggested_pickup, suggested_pickup_period, location, station_id, observation_time,temp_f, temp_c, water_temp_f, water_temp_c, wind_string, wind_dir, wind_degrees, wind_mph, wind_gust_mph, wind_kt, pressure_string, pressure_mb, dewpoint_string, dewpoint_f, dewpoint_c'
) AS (
credit: {(attr:map[], content:chararray)}
credit_URL: {(attr:map[], content:chararray)}
.
.
.
);
dump data;

Cutting a XML with JAXB

I have the following xml:
<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic id="1">
<Size>
<value>1.68</value>
<scale>Meters</scale>
<comment>Size can vary, depending on temperature</comment>
</Size>
<Weight>
<value>9</value>
<scale>M*Tons</scale>
<comment>His mama is so fat, we had to use another scale</comment>
</Weight>
<rating>
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:value companyDepartment="Finance">A</rating:value>
<rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate>
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic id="2">
<Size/>
<Weight/>
<rating/>
</Characteristic>
...
<Characteristic id="n"/>
</PackageBody>
</Package>
And the following Java code:
public class XMLTest {
public static void main(String[] args) throws Exception {
Package currentPackage = new Package();
Package sourcePackage = new Package();
int totalCharacteristics;
PackageBody currentPackageBody = new PackageBody();
Characteristic currentCharacteristic = new Characteristic();
rating currentRating = new rating();
FileInputStream fis = new FileInputStream("sourceFile.xml");
JAXBContext myCurrentContext = JAXBContext.newInstance(Package.class);
Marshaller m = myCurrentContext.createMarshaller();
Unmarshaller um = myCurrentContext.createUnmarshaller();
sourcePackage = (Package)um.unmarshal(fis);
currentPackage.setPackageHeader(sourcePackage.getPackageHeader());
totalCharacteristics = sourcePackage.getPackageBody().getCharacteristics().size();
for (int i = 0; i < totalCharacteristics; i++)
{
currentRating = sourcePackage.getPackageBody().getCharacteristics().get(i).getrating();
}
currentCharacteristic.setrating(currentRating);
currentPackageBody.getCharacteristics().add(currentCharacteristic);
currentPackage.setPackageBody(currentPackageBody);
m.marshal(currentPackage, new File("targetFile.xml"));
fis.close();
}
}
Which gives me the next XML:
<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic id="1">
<rating>
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:value companyDepartment="Finance">A</rating:value>
<rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate>
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic id="2">
<rating/>
</Characteristic>
...
<Characteristic id="n"/>
</PackageBody>
</Package>
And this is what I need:
<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic>
<rating id="1">
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic>
<rating id="2"/>
</Characteristic>
...
<Characteristic/>
</PackageBody>
</Package>
But I have a few questions:
How could I implement a way to read a 4GBs file? (for example, reading it with StAX).
If I want to filter some tags from source to target(as in the last xml), would I have to assign them one by one to the targetFile? Is there any iterator that might allow me to go through all subnodes and assign them?
If the sourceFile changes, would I need to rerun the xjc and recompile the whole project?
Thanks.
For reading huge XML files, you definitely need a streaming parser like StAX. In addition, you can use a combination of JAXB to selectively map a given piece of xml to java object if you wish work with it. You need to regenerate your JAXB classes only if your schema changes. No need to regenerate if you application code changes.

import kml with java

I'm trying to import a mkl file with jak but i get the following error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://earth.google.com/kml/2.2", local:"kml"). Expected elements are
...
and then a big list
Does anyone else run into this problem?
This is the code:
final Kml kml = Kml.unmarshal(new File("../data/Eemskanaal.kml"));
final Placemark placemark = (Placemark) kml.getFeature();
Point point = (Point) placemark.getGeometry();
List<Coordinate> coordinates = point.getCoordinates();
for (Coordinate coordinate : coordinates) {
System.out.println(coordinate.getLatitude());
System.out.println(coordinate.getLongitude());
System.out.println(coordinate.getAltitude());
}
And this is the kml file:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>BU00100107 Verspreide huizen Eemskanaal (ten zuiden)</name>
<description><![CDATA[description]]></description>
<Placemark>
<name>BLA!</name>
<description><![CDATA[]]></description>
<styleUrl>#style1</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
6.941796,53.314914,0.000000
6.942705,53.310923,0.000000
6.952713,53.305394,0.000000
6.954853,53.300262,0.000000
6.954239,53.296317,0.000000
6.962271,53.295483,0.000000
6.995900,53.287338,0.000000
6.995013,53.285264,0.000000
6.996842,53.281429,0.000000
6.991748,53.278255,0.000000
6.990729,53.275234,0.000000
6.988361,53.274477,0.000000
6.990120,53.271780,0.000000
6.984540,53.272709,0.000000
6.984543,53.274393,0.000000
6.980317,53.274404,0.000000
6.975829,53.272503,0.000000
6.974816,53.271125,0.000000
6.963342,53.271937,0.000000
6.955082,53.265909,0.000000
6.945183,53.269634,0.000000
6.940684,53.273351,0.000000
6.935942,53.273875,0.000000
6.934392,53.276351,0.000000
6.929104,53.272181,0.000000
6.909544,53.265952,0.000000
6.908803,53.269015,0.000000
6.909151,53.278897,0.000000
6.888166,53.279161,0.000000
6.887788,53.279639,0.000000
6.886750,53.280950,0.000000
6.886729,53.280977,0.000000
6.888260,53.281856,0.000000
6.895912,53.286254,0.000000
6.892976,53.288089,0.000000
6.891571,53.290803,0.000000
6.887323,53.298046,0.000000
6.887729,53.309725,0.000000
6.887583,53.309816,0.000000
6.888683,53.311891,0.000000
6.893966,53.313119,0.000000
6.924732,53.311548,0.000000
6.929655,53.312392,0.000000
6.934810,53.315353,0.000000
6.941796,53.314914,0.000000
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
6.905549,53.283453,0.000000
6.908790,53.282516,0.000000
6.912146,53.283305,0.000000
6.916480,53.287575,0.000000
6.916764,53.288072,0.000000
6.915251,53.288369,0.000000
6.915097,53.290097,0.000000
6.912526,53.292361,0.000000
6.908052,53.290971,0.000000
6.905569,53.288875,0.000000
6.905549,53.283453,0.000000
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
Any other solutions are also welcome
Here is my quick and dirty way :)
public static Kml getKml(InputStream is) throws Exception {
String str = IOUtils.toString( is );
IOUtils.closeQuietly( is );
str = StringUtils.replace( str, "xmlns=\"http://earth.google.com/kml/2.2\"", "xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\"" );
ByteArrayInputStream bais = new ByteArrayInputStream( str.getBytes( "UTF-8" ) );
return Kml.unmarshal(bais);
}
I am unfamiliar with Jak, but if you're using the OGC Schema, the namespace is different. You have
http://earth.google.com/kml/2.2
The OGC namespace is
http://www.opengis.net/kml/2.2
The Google extension schema uses
http://www.google.com/kml/ext/2.2
as well. The namespace you're using was used by Google before KML was given to the OGC as an open standard.
The namespace is incorrect, but if you have 1700 files and this is the only problem, you might consider simply using the two-argument form of Kml.unmarshal(File file, boolean validate).
final Kml kml = loadXMLFile("../data/Eemskanaal.kml");
private static Kml loadXMLFile(String path) {
Kml kml = null;
try {
kml = Kml.unmarshal(path);
} catch (RuntimeException ex) {
kml = Kml.unmarshal(new File(path), false);
}
return kml;
}
I've also used the following cheezy perl script to correct my files.
$ cat ~/bin/fixxmlns
#!/usr/bin/perl
for (#ARGV) {
open (FH,"<$_");
open (NFH,">$_.x");
$look = 1;
while ($r = <FH>) {
if ($look && $r =~ /earth.google.com/) {
$r = qq{<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2">\n};
$look = 0;
}
print NFH $r;
}
close (NFH);
close (FH);
rename("$_", "$_.orig");
rename("$_.x", "$_");
}
$ fixxmlns *.kml
$ find parentdir -name "*.kml" -print0 | xargs -0 fixxmlns

Categories

Resources