NullPointerException when importing XML-file into Processing, Java - java

I'm trying to get data from an XML-file and use this data in processing. When doing so I get a NPE, and I can't quite figure out where I'm wrong. The XML got several layers and I have to get data from this "child":
http://i62.tinypic.com/2mb90g.png
My code looks like this:
XML xml;
void setup(){
xml = loadXML("parker.xml");
XML[] children = xml.getChildren("kml");
XML[] Folder=children[0].getChildren("Folder");
XML[] Placemark=Folder[1].getChildren("Placemark");
XML[] Polygon=Placemark[2].getChildren("Polygon");
XML[] outerBoundaryIs=Polygon[3].getChildren("outerBoundaryIs");
XML[] LinearRing=outerBoundaryIs[4].getChildren("LinearRing");
for (int i = 0; i < LinearRing.length; i++) {
float coordinates = children[i].getFloat("coordinates");
println(coordinates);
}
}
Best Chris
Stack trace:
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content
is not allowed in prolog. at
com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at processing.data.XML.(XML.java:187) at
processing.core.PApplet.loadXML(PApplet.java:6310) at
processing.core.PApplet.loadXML(PApplet.java:6300) at
XMLtryout.setup(XMLtryout.java:21) at
processing.core.PApplet.handleDraw(PApplet.java:2359) at
processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2254) at
java.lang.Thread.run(Thread.java:744)
XML file:
https://www.dropbox.com/s/xn3thjskhlf2wai/parker.xml

This error maybe caused because missing this at the top of your xml file
<?xml version="1.0" encoding="utf-8"?>
or there's some non-printable garbage at the start of your file.

The 'Content is not allowed in prolog' error indicates that you have some content between the XML declaration and the appearance of the document element, for example
<?xml version="1.0" encoding="utf-8"?>
content here is not allowed
<kml xmlns="http://earth.google.com/kml/2.1">
...
</kml>
The XML file you linked is ok though, so it seems you're
reading the XML binary incorrectly before passing it to the XML parser,
or (more likely) you're not reading the XML at all (can happen when you read from a web URL and getting an error response). I assume you get a HTTP 40x error which you don't recognize, and read the response (usually HTML) as XML, which causes the error. Remember that applets usually can only read resources from the same server (that's what might cause the error).
To verify this, attempt to read the URL content and output it as text, and check if it looks ok.

Make it more easy
try like this
public static void setUp(){
try {
File file = new File("parker.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("LinearRing");
System.out.println("Information");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("coordinates");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("coordinates : " + ((Node) fstNm.item(0)).getNodeValue());
//
//
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
that should display :
Root element kml
Information
coordinates :
10.088512,56.154109,0
10.088512,56.154109,0
10.088512,56.154109,0
10.088511,56.15411,0
10.088511,56.15411,0
10.08851,56.15411,0
10.08851,56.154111,0
10.088509,56.154111,0
10.088508,56.154111,0
10.088508,56.154111,0
10.088507,56.154111,0
10.088506,56.154111,0
10.088506,56.154111,0
10.088505,56.154111,0
10.088504,56.154111,0
10.088504,56.154111,0
10.088503,56.15411,0
10.088503,56.15411,0
10.088502,56.15411,0
10.088502,56.154109,0
10.088502,56.154109,0
10.088501,56.154109,0
10.088501,56.154108,0
10.088501,56.154108,0
10.088501,56.154108,0
10.088501,56.154107,0
10.088285,56.154094,0
10.088104,56.154372,0
10.088061,56.154401,0
10.087988,56.15445,0
10.087915,56.154611,0
10.08791,56.154613,0
10.087912,56.154613,0
10.087877,56.1548,0
10.08772,56.15482,0
10.087558,56.154911,0
10.087421,56.155111,0
10.087316,56.155308,0
10.087328,56.15538,0
10.087372,56.155413,0
10.087446,56.155453,0
10.087484,56.155487,0
10.08747,56.155601,0
10.08772,56.155616,0
10.087719,56.155618,0
10.088618,56.155671,0
10.089096,56.1557,0
10.089096,56.155699,0
10.089138,56.155701,0
10.089127,56.155706,0
10.089004,56.155787,0
10.08888,56.155853,0
10.088799,56.155806,0
10.088571,56.155914,0
10.088455,56.155946,0
10.088112,56.156081,0
10.088184,56.156138,0
10.087733,56.156353,0
10.087489,56.156421,0
10.087288,56.156341,0
10.087268,56.156333,0
10.086893,56.156182,0
10.08684,56.156271,0
10.087049,56.156373,0
10.086893,56.156455,0
10.086664,56.156575,0
10.086443,56.156698,0
10.086425,56.156708,0
10.085983,56.156955,0
10.085655,56.157139,0
10.085462,56.157276,0
10.085272,56.157233,0
10.085176,56.157328,0
10.084917,56.157393,0
10.084883,56.157458,0
10.08495,56.157513,0
10.084947,56.157524,0
10.084943,56.157539,0
10.084855,56.15787,0
10.084855,56.15787,0
10.084321,56.157317,0
10.085553,56.156195,0
10.085555,56.156194,0
10.085553,56.156194,0
10.085734,56.156035,0
10.085821,56.155977,0
10.085937,56.155932,0
10.085993,56.155942,0
10.086031,56.155959,0
10.086171,56.15592,0
10.086227,56.155901,0
10.086392,56.155841,0
10.086513,56.155786,0
10.08664,56.155699,0
10.086686,56.155657,0
10.086727,56.155605,0
10.086777,56.155486,0
10.086861,56.155289,0
10.086916,56.155134,0
10.087006,56.154899,0
10.087075,56.154706,0
10.087094,56.154649,0
10.0871,56.154574,0
10.087112,56.154464,0
10.087111,56.154362,0
10.087112,56.154279,0
10.087112,56.154279,0
10.087113,56.15427,0
10.087114,56.154198,0
10.087108,56.15413,0
10.087091,56.154054,0
10.086992,56.153698,0
10.087,56.153678,0
10.087031,56.153647,0
10.087036,56.153648,0
10.087046,56.153652,0
10.087035,56.153647,0
10.087039,56.153645,0
10.087072,56.153612,0
10.087367,56.153308,0
10.087371,56.153303,0
10.08742,56.15323,0
10.087568,56.152963,0
10.087568,56.152962,0
10.087569,56.152962,0
10.08757,56.152961,0
10.087571,56.15296,0
10.087573,56.152959,0
10.087574,56.152959,0
10.087575,56.152958,0
10.087577,56.152958,0
10.087579,56.152958,0
10.087581,56.152957,0
10.087582,56.152957,0
10.087584,56.152957,0
10.087586,56.152958,0
10.087588,56.152958,0
10.087589,56.152958,0
10.087591,56.152959,0
10.087592,56.152959,0
10.087593,56.15296,0
10.087594,56.152961,0
10.087595,56.152962,0
10.087596,56.152963,0
10.087596,56.152964,0
10.087596,56.152965,0
10.087596,56.152965,0
10.087614,56.152967,0
10.087921,56.152988,0
10.088134,56.153019,0
10.088311,56.15304,0
10.088454,56.153052,0
10.088469,56.153378,0
10.08847,56.153445,0
10.088473,56.153597,0
10.088473,56.153597,0
10.088473,56.153597,0
10.088703,56.153614,0
10.088703,56.153614,0
10.088703,56.153614,0
10.088704,56.153614,0
10.088705,56.153614,0
10.088705,56.153615,0
10.088706,56.153615,0
10.088706,56.153615,0
10.088707,56.153616,0
10.088707,56.153616,0
10.088707,56.153616,0
10.088707,56.153617,0
10.088707,56.153617,0
10.088707,56.153617,0
10.088707,56.153618,0
10.088512,56.154108,0
10.088512,56.154109,0
coordinates :
10.086779,56.155487,0
10.086778,56.155488,0
10.086779,56.155487,0
10.086779,56.155487,0
coordinates :
10.08847,56.153602,0
10.088469,56.153602,0
10.088469,56.153602,0
10.08847,56.153602,0
PS : kml is the root element

Related

Netconf: Not able to perform getConfig()

We have a netconf enabled router and I am able to perform get/get-config operations using Mg-Soft client browser. This is the output of getConfig() -
REQ:
<nc:get-config xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<nc:source>
<nc:running/>
</nc:source>
</nc:get-config>
RESP:
<?xml version="1.0" encoding="utf-8"?>
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
<qos>
<sap-ingress>
.....
</data>
But I am unable to perform the same operation using tailf Netconf client.
Here is my code -
public class Client {
public static void main(String args[]) throws JNCException,
ParserConfigurationException, SAXException, IOException {
DeviceUser deviceUser;
String deviceUserName = "user";
deviceUser = new DeviceUser(deviceUserName, "admin", "admin");
Device device = new Device("simulator", deviceUser, "10.135.16.13", 830);
device.connect(deviceUserName);
device.newSession(new Subscriber("simulator"), "mySession");
NetconfSession session = device.getSession("mySession");
NodeSet config = session.getConfig(NetconfSession.RUNNING);
String xmlString = config.toXMLString();
session.closeSession();
System.out.println(xmlString);
}
}
class Subscriber extends IOSubscriber {
String devName;
public static boolean rawMode = false;
Subscriber(String devName) {
super(rawMode);
this.devName = devName;
}
public void input(String data) {
System.out.println("RECV: " + data);
}
public void output(String data) {
System.out.println("SEND: " + data);
}
}
I am getting the following in the eclipse console -
SEND: <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
</capabilities>
</hello>
RECV: <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
<capability>urn:ietf:params:netconf:base:1.1</capability>
<capability>urn:ietf:params:netconf:capability:writable-running:1.0</capability>
<capability>urn:ietf:params:netconf:capability:candidate:1.0</capability>
<capability>urn:ietf:params:netconf:capability:validate:1.0</capability>
<capability>urn:ietf:params:netconf:capability:validate:1.1</capability>
<capability>urn:ietf:params:netconf:capability:startup:1.0</capability>
<capability>urn:ietf:params:netconf:capability:url:1.0?scheme=ftp,tftp,file</capability>
<capability>urn:ietf:params:netconf:capability:with-defaults:1.0?basic-mode=trim&also-supported=report-all</capability>
<capability>urn:ietf:params:xml:ns:netconf:base:1.0?module=ietf-netconf&revision=2011-06-01&features=writable-running,validate,startup,url&deviations=alu-netconf-deviations-r13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:netconf-deviations-r13?module=alu-netconf-deviations-r13&revision=2015-01-23</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:cli-content-layer-r13?module=alu-cli-content-layer-r13&revision=2015-01-23</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-r13?module=alu-conf-r13&revision=2016-06-24</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-aaa-r13?module=alu-conf-aaa-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-aa-r13?module=alu-conf-aa-r13&revision=2016-06-23</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-calltrace-r13?module=alu-conf-calltrace-r13&revision=2016-04-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-card-r13?module=alu-conf-card-r13&revision=2015-10-30</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-cflowd-r13?module=alu-conf-cflowd-r13&revision=2015-01-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-conn-prof-r13?module=alu-conf-conn-prof-r13&revision=2015-11-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-con-profvlan-r13?module=alu-conf-con-profvlan-r13&revision=2015-08-28</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-eth-cfm-r13?module=alu-conf-eth-cfm-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-eth-ring-r13?module=alu-conf-eth-ring-r13&revision=2014-10-17</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-eth-tunnel-r13?module=alu-conf-eth-tunnel-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-filter-r13?module=alu-conf-filter-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-gmpls-tunnel-r13?module=alu-conf-gmpls-tunnel-r13&revision=2014-12-09</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-ifgrphandler-r13?module=alu-conf-ifgrphandler-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-isa-r13?module=alu-conf-isa-r13&revision=2016-02-29</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-lag-r13?module=alu-conf-lag-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-li-r13?module=alu-conf-li-r13&revision=2016-04-21</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-log-r13?module=alu-conf-log-r13&revision=2016-06-01</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-mcastmgmt-r13?module=alu-conf-mcastmgmt-r13&revision=2015-03-12</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-mirror-r13?module=alu-conf-mirror-r13&revision=2016-01-08</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-oam-pm-r13?module=alu-conf-oam-pm-r13&revision=2015-11-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-open-flow-r13?module=alu-conf-open-flow-r13&revision=2015-11-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-r13?module=alu-conf-port-r13&revision=2015-08-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-access-r13?module=alu-conf-port-access-r13&revision=2014-10-31</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-aps-r13?module=alu-conf-port-aps-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-ethernet-r13?module=alu-conf-port-ethernet-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-mbundle-r13?module=alu-conf-port-mbundle-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-network-r13?module=alu-conf-port-network-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-sonetsdh-r13?module=alu-conf-port-sonetsdh-r13&revision=2015-06-11</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-tdm-r13?module=alu-conf-port-tdm-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-tdm-ds1-r13?module=alu-conf-port-tdm-ds1-r13&revision=2014-12-15</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-tdm-ds3-r13?module=alu-conf-port-tdm-ds3-r13&revision=2014-12-15</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-tdm-e1-r13?module=alu-conf-port-tdm-e1-r13&revision=2014-12-15</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-tdm-e3-r13?module=alu-conf-port-tdm-e3-r13&revision=2014-12-15</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-port-policy-r13?module=alu-conf-port-policy-r13&revision=2015-12-09</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-pxc-r13?module=alu-conf-pxc-r13&revision=2015-04-24</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-python-r13?module=alu-conf-python-r13&revision=2016-06-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-qos-r13?module=alu-conf-qos-r13&revision=2016-04-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-redundancy-r13?module=alu-conf-redundancy-r13&revision=2016-03-25</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-r13?module=alu-conf-router-r13&revision=2016-05-09</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-bgp-r13?module=alu-conf-router-bgp-r13&revision=2016-06-06</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-dhcps-r13?module=alu-conf-router-dhcps-r13&revision=2015-09-21</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-dhcp6-r13?module=alu-conf-router-dhcp6-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-fspec-r13?module=alu-conf-router-fspec-r13&revision=2015-12-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-gmpls-r13?module=alu-conf-router-gmpls-r13&revision=2015-11-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-igmp-r13?module=alu-conf-router-igmp-r13&revision=2015-11-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-if-r13?module=alu-conf-router-if-r13&revision=2016-04-29</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-isis-r13?module=alu-conf-router-isis-r13&revision=2016-02-18</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-l2tp-r13?module=alu-conf-router-l2tp-r13&revision=2016-05-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-ldp-r13?module=alu-conf-router-ldp-r13&revision=2016-05-02</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-lmp-r13?module=alu-conf-router-lmp-r13&revision=2015-09-08</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-mccacpl-r13?module=alu-conf-router-mccacpl-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-mld-r13?module=alu-conf-router-mld-r13&revision=2015-09-22</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-mpls-r13?module=alu-conf-router-mpls-r13&revision=2016-04-28</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-mpls-tp-r13?module=alu-conf-router-mpls-tp-r13&revision=2015-11-09</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-nat-r13?module=alu-conf-router-nat-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-ogvalid-r13?module=alu-conf-router-ogvalid-r13&revision=2015-11-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-ospf-r13?module=alu-conf-router-ospf-r13&revision=2016-06-22</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-ospf3-r13?module=alu-conf-router-ospf3-r13&revision=2016-06-22</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-pcep-r13?module=alu-conf-router-pcep-r13&revision=2016-03-07</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-pim-r13?module=alu-conf-router-pim-r13&revision=2016-06-07</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-policy-r13?module=alu-conf-router-policy-r13&revision=2016-05-19</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-radprox-r13?module=alu-conf-router-radprox-r13&revision=2015-11-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-rip-r13?module=alu-conf-router-rip-r13&revision=2015-06-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-nhroute-r13?module=alu-conf-router-nhroute-r13&revision=2014-09-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-radvert-r13?module=alu-conf-router-radvert-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-rsvp-r13?module=alu-conf-router-rsvp-r13&revision=2015-06-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-wlan-r13?module=alu-conf-router-wlan-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-router-wpp-r13?module=alu-conf-router-wpp-r13&revision=2015-03-04</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-saa-r13?module=alu-conf-saa-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-service-r13?module=alu-conf-service-r13&revision=2016-03-23</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-apipe-r13?module=alu-conf-svc-apipe-r13&revision=2016-02-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-cpipe-r13?module=alu-conf-svc-cpipe-r13&revision=2016-02-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-cust-r13?module=alu-conf-svc-cust-r13&revision=2015-09-23</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-dynsrv-r13?module=alu-conf-svc-dynsrv-r13&revision=2015-09-11</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-egrmcgroup-r13?module=alu-conf-svc-egrmcgroup-r13&revision=2015-09-11</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-epipe-r13?module=alu-conf-svc-epipe-r13&revision=2016-06-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-fpipe-r13?module=alu-conf-svc-fpipe-r13&revision=2016-02-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-ies-r13?module=alu-conf-svc-ies-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-ies-if-r13?module=alu-conf-svc-ies-if-r13&revision=2016-06-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-ies-subif-r13?module=alu-conf-svc-ies-subif-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-ipfix-r13?module=alu-conf-svc-ipfix-r13&revision=2014-09-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-ipipe-r13?module=alu-conf-svc-ipipe-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-nat-r13?module=alu-conf-svc-nat-r13&revision=2016-04-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-sdp-r13?module=alu-conf-svc-sdp-r13&revision=2016-05-09</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-template-r13?module=alu-conf-svc-template-r13&revision=2016-04-06</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-upnp-r13?module=alu-conf-svc-upnp-r13&revision=2015-09-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-r13?module=alu-conf-svc-vpls-r13&revision=2016-06-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-igmp-r13?module=alu-conf-svc-vpls-igmp-r13&revision=2014-09-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-macmo-r13?module=alu-conf-svc-vpls-macmo-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-macpr-r13?module=alu-conf-svc-vpls-macpr-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-meshs-r13?module=alu-conf-svc-vpls-meshs-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-mld-r13?module=alu-conf-svc-vpls-mld-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-pim-r13?module=alu-conf-svc-vpls-pim-r13&revision=2015-07-07</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-sap-r13?module=alu-conf-svc-vpls-sap-r13&revision=2016-06-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-split-r13?module=alu-conf-svc-vpls-split-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-spoke-r13?module=alu-conf-svc-vpls-spoke-r13&revision=2016-06-13</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-spb-r13?module=alu-conf-svc-vpls-spb-r13&revision=2016-02-04</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vpls-stp-r13?module=alu-conf-svc-vpls-stp-r13&revision=2014-09-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-r13?module=alu-conf-svc-vprn-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-bgp-r13?module=alu-conf-svc-vprn-bgp-r13&revision=2016-05-04</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-dhcps-r13?module=alu-conf-svc-vprn-dhcps-r13&revision=2015-09-21</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-dhcp6-r13?module=alu-conf-svc-vprn-dhcp6-r13&revision=2016-06-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-fspec-r13?module=alu-conf-svc-vprn-fspec-r13&revision=2016-02-11</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-igmp-r13?module=alu-conf-svc-vprn-igmp-r13&revision=2015-11-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-if-r13?module=alu-conf-svc-vprn-if-r13&revision=2016-06-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-isis-r13?module=alu-conf-svc-vprn-isis-r13&revision=2016-02-18</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-l2tp-r13?module=alu-conf-svc-vprn-l2tp-r13&revision=2016-05-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-mld-r13?module=alu-conf-svc-vprn-mld-r13&revision=2015-10-14</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-nat-r13?module=alu-conf-svc-vprn-nat-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-nwif-r13?module=alu-conf-svc-vprn-nwif-r13&revision=2016-04-29</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svs-vprn-ntp-r13?module=alu-conf-svs-vprn-ntp-r13&revision=2015-05-05</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-ospf-r13?module=alu-conf-svc-vprn-ospf-r13&revision=2016-06-22</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-ospf3-r13?module=alu-conf-svc-vprn-ospf3-r13&revision=2016-06-22</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-pim-r13?module=alu-conf-svc-vprn-pim-r13&revision=2015-08-17</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-ptp-r13?module=alu-conf-svc-vprn-ptp-r13&revision=2015-05-22</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-vprn-radprox-r13?module=alu-conf-vprn-radprox-r13&revision=2015-06-23</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-rip-r13?module=alu-conf-svc-vprn-rip-r13&revision=2015-06-03</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-ripng-r13?module=alu-conf-svc-vprn-ripng-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-radv-r13?module=alu-conf-svc-vprn-radv-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-srcad-r13?module=alu-conf-svc-vprn-srcad-r13&revision=2015-01-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-spoke-r13?module=alu-conf-svc-vprn-spoke-r13&revision=2015-12-17</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-subif-r13?module=alu-conf-svc-vprn-subif-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-wlan-r13?module=alu-conf-svc-vprn-wlan-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-svc-vprn-wpp-r13?module=alu-conf-svc-vprn-wpp-r13&revision=2015-03-04</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-sflow-r13?module=alu-conf-sflow-r13&revision=2014-09-26</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-sfm-r13?module=alu-conf-sfm-r13&revision=2014-10-20</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-subscrmgmt-r13?module=alu-conf-subscrmgmt-r13&revision=2016-06-27</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-submgmt-ludb-r13?module=alu-conf-submgmt-ludb-r13&revision=2016-06-14</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-submgmt-wlangw-r13?module=alu-conf-submgmt-wlangw-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-system-r13?module=alu-conf-system-r13&revision=2016-06-08</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-sys-pwrmgmt-r13?module=alu-conf-sys-pwrmgmt-r13&revision=2015-05-25</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-sys-ptp-r13?module=alu-conf-sys-ptp-r13&revision=2015-06-25</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-sys-sec-r13?module=alu-conf-sys-sec-r13&revision=2016-06-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-vrrp-r13?module=alu-conf-vrrp-r13&revision=2015-10-14</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:conf-vsm-r13?module=alu-conf-vsm-r13&revision=2015-09-16</capability>
<capability>urn:alcatel-lucent.com:sros:ns:yang:types?module=alu-types&revision=2015-01-19</capability>
<capability>urn:nokia.com:sros:ns:yang:sr:conf?module=nokia-conf&revision=2016-01-01</capability>
<capability>urn:nokia.com:sros:ns:yang:sr:sros-yang-extensions?module=nokia-sros-yang-extensions&revision=2016-01-01</capability>
<capability>urn:nokia.com:sros:ns:yang:sr:types-filter?module=nokia-types-filter&revision=2016-01-01</capability>
<capability>urn:nokia.com:sros:ns:yang:sr:types-qos?module=nokia-types-qos&revision=2016-01-01</capability>
<capability>urn:nokia.com:sros:ns:yang:sr:types-services?module=nokia-types-services&revision=2016-01-01</capability>
<capability>urn:nokia.com:sros:ns:yang:sr:types-sros?module=nokia-types-sros&revision=2016-01-01</capability>
<capability>urn:nokia.com:sros:ns:yang:sr:major-release-14</capability>
</capabilities>
<session-id>52</session-id>
</hello>
SEND: <nc:rpc xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:message-id="1">
<nc:get-config>
<nc:source>
<nc:running/> </nc:source>
</nc:get-config>
</nc:rpc>
RECV: <?xml version="1.0" encoding="UTF-8"?>
<rpc-reply urn:ietf:params:xml:ns:netconf:base:1.0:message-id="1" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<rpc-error>
<error-type>rpc</error-type>
<error-tag>missing-attribute</error-tag>
<error-severity>error</error-severity>
<error-info>
<bad-element>message-id</bad-element>
<bad-element>rpc</bad-element>
</error-info>
<error-message>
Check that message-id attribute is present and less than 4096 characters
</error-message>
</rpc-error>
</rpc-reply>
[Fatal Error] :2:20: Attribute name "urn:ietf" associated with an element type "rpc-reply" must be followed by the ' = ' character.
[Fatal Error] :2:20: Attribute name "urn:ietf" associated with an element type "rpc-reply" must be followed by the ' = ' character.
org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 20; Attribute name "urn:ietf" associated with an element type "rpc-reply" must be followed by the ' = ' character.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
at com.tailf.jnc.YangXMLParser.parse(YangXMLParser.java:50)
at com.tailf.jnc.XMLParser.parse(XMLParser.java:154)
at com.tailf.jnc.NetconfSession.recv_rpc_reply(NetconfSession.java:1495)
at com.tailf.jnc.NetconfSession.recv_rpc_reply_data(NetconfSession.java:1472)
at com.tailf.jnc.NetconfSession.getConfig(NetconfSession.java:443)
at client.Client.main(Client.java:51)
Exception in thread "main" Parse error: parse error: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 20; Attribute name "urn:ietf" associated with an element type "rpc-reply" must be followed by the ' = ' character.
at com.tailf.jnc.YangXMLParser.parse(YangXMLParser.java:54)
at com.tailf.jnc.XMLParser.parse(XMLParser.java:154)
at com.tailf.jnc.NetconfSession.recv_rpc_reply(NetconfSession.java:1495)
at com.tailf.jnc.NetconfSession.recv_rpc_reply_data(NetconfSession.java:1472)
at com.tailf.jnc.NetconfSession.getConfig(NetconfSession.java:443)
at client.Client.main(Client.java:51)
You actually seem to have two problems, neither of which appear to be your fault. The get-config request from JNC seems to be malformed: message-id attribute should be in the default namespace, not in NETCONF message namespace.
SEND: <nc:rpc xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:message-id="1">
<nc:get-config>
<nc:source>
<nc:running/> </nc:source>
</nc:get-config>
</nc:rpc>
Should actually be:
SEND: <nc:rpc xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
<nc:get-config>
<nc:source>
<nc:running/> </nc:source>
</nc:get-config>
</nc:rpc>
The second problem is your server, which appears to be unable to handle this malformed request - and is responding with a malformed message (it is not even an XML message):
RECV: <?xml version="1.0" encoding="UTF-8"?>
<rpc-reply urn:ietf:params:xml:ns:netconf:base:1.0:message-id="1" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<rpc-error>
<error-type>rpc</error-type>
<error-tag>missing-attribute</error-tag>
<error-severity>error</error-severity>
<error-info>
<bad-element>message-id</bad-element>
<bad-element>rpc</bad-element>
</error-info>
<error-message>
Check that message-id attribute is present and less than 4096 characters
</error-message>
</rpc-error>
</rpc-reply>
It should be responding with:
RECV: <?xml version="1.0" encoding="UTF-8"?>
<rpc-reply nc:message-id="1" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<rpc-error>
<error-type>rpc</error-type>
<error-tag>missing-attribute</error-tag>
<error-severity>error</error-severity>
<error-info>
<bad-element>message-id</bad-element>
<bad-element>rpc</bad-element>
</error-info>
<error-message>
Check that message-id attribute is present and less than 4096 characters
</error-message>
</rpc-error>
</rpc-reply>
since it is supposed to return all attributes verbatim. Otherwise, this would be standard behavior when a message without a message-id attribute is sent to a server.
Relevant RFC4741 text below (the same applies to RFC6241, NETCONF 1.1).
Section 4.1:
The <rpc> element has a mandatory attribute "message-id", which is an
arbitrary string chosen by the sender of the RPC that will commonly
encode a monotonically increasing integer. The receiver of the RPC
does not decode or interpret this string but simply saves it to be
used as a "message-id" attribute in any resulting <rpc-reply>
message. For example:
<rpc message-id="101"
xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<some-method>
<!-- method parameters here... -->
</some-method>
</rpc>
If additional attributes are present in an <rpc> element, a NETCONF
peer MUST return them unmodified in the <rpc-reply> element.
Section 4.3
An error is returned if an <rpc> element is received without a
message-id attribute. Note that only in this case is it
acceptable for the NETCONF peer to omit the message-id attribute
in the <rpc-reply> element.
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<get-config>
<source>
<running/>
</source>
</get-config>
</rpc>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<rpc-error>
<error-type>rpc</error-type>
<error-tag>missing-attribute</error-tag>
<error-severity>error</error-severity>
<error-info>
<bad-attribute>message-id</bad-attribute>
<bad-element>rpc</bad-element>
</error-info>
</rpc-error>
</rpc-reply>
So I believe you should be reporting two issues to both the maker of the device and to creators of JNC.
The reason it is working with the MG-SOFT client is that it sends a proper message-id attribute, so your case never happens.

XPath Java parsing xml document under more conditions

XPath Java parsing xml under more conditions
I need to show elements from books.xml which satisfy next two
conditions: price > 10 and publish_date > "2006-12-31" . books.xml is:
<?xml version='1.0'?>
<catalog>
<book id='bk110'>
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2006-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id='bk111'>
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2007-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id='bk112'>
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2008-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
When I try this code:
package web.services;
import java.io.File;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.xpath.*;
import org.xml.sax.*;
import org.w3c.dom.*;
public class WebServices {
private static void showElements() {
InputSource inputSource = null;
Object result;
NodeList nodeList = null;
String file;
String workingDir = System.getProperty("user.dir");
file="data"+File.separator+"books.xml";
try {
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression xPathExpression = xPath.compile("//book[price > 10][xs:date(publish_date) > xs:date('2005-12-31')]/*/text()");
File xmlDocument = new File(file);
try {
inputSource = new InputSource(new FileInputStream(xmlDocument));
} catch (FileNotFoundException ex) {
Logger.getLogger(WebServices.class.getName()).log(Level.SEVERE, null, ex);
}
result = xPathExpression.evaluate(inputSource, XPathConstants.NODESET);
nodeList = (NodeList) result;
} catch (XPathExpressionException ex) {
Logger.getLogger(WebServices.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.print("Node name: " + nodeList.item(i).getNodeName());
System.out.print(" | ");
System.out.println("Node value: " + nodeList.item(i).getNodeValue());
System.out.println("------------------------------------------------");
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
showElements();
}
}
I'm getting this error:
maj 27, 2015 10:01:19 AM web.services.WebServices showElements
SEVERE: null
javax.xml.transform.TransformerException: Unknown error in XPath.
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:368)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:305)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:135)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:109)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:303)
at web.services.WebServices.showElements(WebServices.java:39)
at web.services.WebServices.main(WebServices.java:58)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xpath.internal.functions.FuncExtFunction.execute(FuncExtFunction.java:210)
at com.sun.org.apache.xpath.internal.Expression.execute(Expression.java:157)
at com.sun.org.apache.xpath.internal.operations.Operation.execute(Operation.java:111)
at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.executePredicates(PredicatedNodeTest.java:344)
at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:481)
at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(AxesWalker.java:374)
at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(WalkingIterator.java:197)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(NodeSequence.java:344)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(NodeSequence.java:503)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:279)
at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:214)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:339)
... 6 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xpath.internal.functions.FuncExtFunction.execute(FuncExtFunction.java:210)
at com.sun.org.apache.xpath.internal.Expression.execute(Expression.java:157)
at com.sun.org.apache.xpath.internal.operations.Operation.execute(Operation.java:111)
at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.executePredicates(PredicatedNodeTest.java:344)
at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:481)
at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(AxesWalker.java:374)
at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(WalkingIterator.java:197)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(NodeSequence.java:344)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(NodeSequence.java:503)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:279)
at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:214)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:339)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:305)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:135)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:109)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:303)
at web.services.WebServices.showElements(WebServices.java:39)
at web.services.WebServices.main(WebServices.java:58)
--------------- linked to ------------------
javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: Unknown error in XPath.
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:305)
at web.services.WebServices.showElements(WebServices.java:39)
at web.services.WebServices.main(WebServices.java:58)
Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:368)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:305)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:135)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:109)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:303)
... 2 more
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xpath.internal.functions.FuncExtFunction.execute(FuncExtFunction.java:210)
at com.sun.org.apache.xpath.internal.Expression.execute(Expression.java:157)
at com.sun.org.apache.xpath.internal.operations.Operation.execute(Operation.java:111)
at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.executePredicates(PredicatedNodeTest.java:344)
at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:481)
at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(AxesWalker.java:374)
at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(WalkingIterator.java:197)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(NodeSequence.java:344)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(NodeSequence.java:503)
at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:279)
at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:214)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:339)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at web.services.WebServices.showElements(WebServices.java:45)
at web.services.WebServices.main(WebServices.java:58)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
What is wrong? Thank you!
You are trying to use XPath 2.0 data types like xs:date while the XPath implementation in the Oracle JRE only supports XPath 1.0 which does not know any such data types. For that particular path expression it should be possible to use XPath 1.0 and simple number comparison with a path like //book[price > 10][number(translate(publish_date, '-', '')) > 20051231].
If you want to use XPath 2.0 you need to look into third party libraries like Saxon 9 or into XQuery implementations (as XPath 2.0 is a subset of XQuery 1.0).

Java code for parsing a complex XML file

Below is a program written to parse the accompanying XML file.
Problem: There are two lane-IDs, but the output of my code keeps outputting only one. I expect the output to be:
Current Element: detector-report
Detector ID : I-74 NB from 12th Ave to 7th Ave
Status : operational
Lane-ID : 1
Lane - ID: 2
The current code still outputs 1 on the last line.
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class FinalTrial {
public static void main(String argv[]) {
try {
File fXmlFile = new File("workFile_09282014 22-10-34c.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("detector-report");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Detector ID : " + eElement.getElementsByTagName("detector-id").item(0).getTextContent());
System.out.println("Status : " + eElement.getElementsByTagName("status").item(0).getTextContent());
NodeList lanes = eElement.getElementsByTagName("lane");
System.out.println(lanes.getLength());
for (int j = 0; j<lanes.getLength(); j++) {
Element lane = (Element) lanes.item(0);
System.out.println("Lane-ID : " + lane.getElementsByTagName("lane-id").item(0).getTextContent());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Below is the accompanying XML file:
<?xml version="1.0" encoding="utf-8"?>
<trafficDetectorData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dummy-temp-address" xmlns:lrms="http://www.dummy-lrms-address" xsi:schemaLocation="http://www.dummy-temp-address C:\temp\test\Detectors.xsd">
<detector-report>
<detector-id>I-74 NB from Ave of the Cities t</detector-id>
<status>operational</status>
<lane>
<lane-id>1</lane-id>
<count>1</count>
<volume>1</volume>
<occupancy>0</occupancy>
<speed>122</speed>
<classes>
<class>
<class-id>Small</class-id>
<count>1</count>
<volume>1</volume>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
<lane>
<lane-id>2</lane-id>
<occupancy>0</occupancy>
<speed>137</speed>
<classes>
<class>
<class-id>Small</class-id>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
</detector-report>
<detector-report>
<detector-id>I-74 NB from 12th Ave to 7th Ave</detector-id>
<status>operational</status>
<lane>
<lane-id>1</lane-id>
<count>3</count>
<volume>3</volume>
<occupancy>3</occupancy>
<speed>100</speed>
<classes>
<class>
<class-id>Small</class-id>
<count>3</count>
<volume>3</volume>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
<lane>
<lane-id>2</lane-id>
<count>4</count>
<volume>4</volume>
<occupancy>3</occupancy>
<speed>116</speed>
<classes>
<class>
<class-id>Small</class-id>
<count>4</count>
<volume>4</volume>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
</detector-report>
</trafficDetectorData>
I urge most developers to use established libraries for XML document handling, to avoid errors such as not properly escaping attribute values.
Java's built-in XML parsing library works very well (org.w3c.dom.Document, etc...).

Using Hapi on an Axis service: "Can't create XML document"

I'm developing an Axis service to interface with a HL7 remote service. I have a class (Hl7MessageTranslator) to encode query request like this:
public Hl7MessageTranslator(...)
{
...
parser = new DefaultXMLParser();
}
public String encodeRequest(...) throws ... HL7Exception
{
// prepare HL7 query request
QRY_A19 qryA19 = new QRY_A19();
...
return parser.encode(qryA19);
}
the encodeRequest() method works fine when called outside the tomcat environment (JUnit test) but fails when called from the Axis service:
I get the following warning while creating the class:
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
INFO - 2014-05-20 09:11:07,543 - Classe: ca.uhn.hl7v2.util.Home - Metodo: setHomeDirectory - Descrizione: hapi.home is set to C:\Programmi\eclipse-j2ee-helios\.
INFO - 2014-05-20 09:11:07,933 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: printHapiVersion - Descrizione: HAPI version is: 2.2
INFO - 2014-05-20 09:11:07,949 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkStructureLibraries - Descrizione: Default Structure libraries found for HL7 versions 2.5, 2.6,
WARN - 2014-05-20 09:11:08,011 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkDOMImplementation - Descrizione: Error occured while trying to retrieve a DOMImplementation.
java.lang.RuntimeException: java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl cannot be cast to org.w3c.dom.DOMImplementationSource
at ca.uhn.hl7v2.util.XMLUtils.getDOMImpl(XMLUtils.java:55)
at ca.uhn.hl7v2.VersionLogger.checkDOMImplementation(VersionLogger.java:44)
at ca.uhn.hl7v2.VersionLogger.init(VersionLogger.java:36)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:126)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:112)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:103)
at ca.uhn.hl7v2.parser.Parser.<init>(Parser.java:71)
at ca.uhn.hl7v2.parser.XMLParser.<init>(XMLParser.java:89)
at ca.uhn.hl7v2.parser.DefaultXMLParser.<init>(DefaultXMLParser.java:81)
at com.avelco.integrazioni.areavasta.naar.Hl7MessageTranslator.<init>(Hl7MessageTranslator.java:105)
at com.avelco.integrazioni.areavasta.naar.NaarBridge.requestGrid(NaarBridge.java:135)
at com.avelco.integration.services.Service.requestGrid(Service.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
...
WARN - 2014-05-20 09:11:08,011 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkDOMImplementation - Descrizione: XML parsing and encoding as well as working with Conformance Profiles will fail.
and the following error while calling the parser.encode() method:
20/05/2014 09:11:08 : debug: Filling MSH ...
20/05/2014 09:11:08 : debug: Filling QRD ...
20/05/2014 09:11:08 : debug: Filling QRF ...
ca.uhn.hl7v2.HL7Exception: Can't create XML document - java.lang.RuntimeException
at ca.uhn.hl7v2.parser.DefaultXMLParser.encodeDocument(DefaultXMLParser.java:115)
at ca.uhn.hl7v2.parser.XMLParser.doEncode(XMLParser.java:241)
at ca.uhn.hl7v2.parser.Parser.encode(Parser.java:276)
at com.avelco.integrazioni.areavasta.naar.Hl7MessageTranslator.encodeRequest(Hl7MessageTranslator.java:174)
at com.avelco.integrazioni.areavasta.naar.NaarBridge.requestGrid(NaarBridge.java:144)
at com.avelco.integration.services.Service.requestGrid(Service.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
Since it works when called by JUnit it must be a dependency/configuration problem but I can't figure where to look for it.
Thanks in advance
Massimo
It is a jar conflict issue with axis dependencies. If you have any seperate log4j jar remove it.
At last I came out of this.
As Hussein Zawawi suggested it was a jar conflict with axis, only it was Xerces and not log4j the cause.
It seems that some HAPI DefaultXMLParser methods expected org.w3c.dom Objects but the factory in my environment produced org.apache.xerces.dom ones.
Unfortunately I couldn't remove Xerces from the project without breaking the legacy software I'm working with. Eventually I had to write my own implementation of DefaultXMLParser rewriting faulty methods and replacing XML parse and serializing calls.
public class AvXmlParser extends DefaultXMLParser
{
/*
* (non-Javadoc)
* #see ca.uhn.hl7v2.parser.DefaultXMLParser#encodeDocument(ca.uhn.hl7v2.model.Message)
*/
public Document encodeDocument(Message source) throws HL7Exception
{
String messageClassName = source.getClass().getName();
String messageName = messageClassName.substring(messageClassName.lastIndexOf('.') + 1);
org.w3c.dom.Document doc = null;
try
{
// doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc = XmlUtilities.emptyDom();
// Element root = doc.createElement(messageName);
Element root = doc.createElementNS("urn:hl7-org:v2xml",messageName);
doc.appendChild(root);
}
catch (Exception e)
{
throw new HL7Exception("Can't create XML document - " + e.getClass().getName(), e);
}
encode(source, doc.getDocumentElement());
return doc;
}
/**
* #param groupObject
* #param groupElement
* #throws HL7Exception
*/
private void encode(ca.uhn.hl7v2.model.Group groupObject, org.w3c.dom.Element groupElement) throws HL7Exception
{
String[] childNames = groupObject.getNames();
String messageName = groupObject.getMessage().getName();
try
{
for (int i = 0; i < childNames.length; i++)
{
Structure[] reps = groupObject.getAll(childNames[i]);
for (int j = 0; j < reps.length; j++)
{
Element childElement = groupElement.getOwnerDocument().createElement(makeGroupElementName(messageName, childNames[i]));
groupElement.appendChild(childElement);
if (reps[j] instanceof Group)
{
encode((Group) reps[j], childElement);
}
else if (reps[j] instanceof Segment)
{
encode((Segment) reps[j], childElement);
}
}
}
}
catch (DOMException e)
{
throw new HL7Exception("Can't encode group " + groupObject.getClass().getName(), e);
}
}
/*
* (non-Javadoc)
* #see ca.uhn.hl7v2.parser.XMLParser#parseStringIntoDocument(java.lang.String)
*/
protected synchronized Document parseStringIntoDocument(String message) throws HL7Exception
{
try
{
Document doc = XmlUtilities.parseString(message);
return doc;
}
catch (Exception e)
{
throw new HL7Exception("Exception parsing XML",e);
}
}
}
(The XmlUtilities class is a utility class already available in my environment).
Most of the problems were so because of the legacy software I'm working with and not because of HAPI or Xerces but I hope my solution can be useful in future.

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