Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Wsdl String Parsing Problem

string

  • Please log in to reply
7 replies to this topic

#1 tucano

tucano

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 27 July 2012 - 10:13 AM

Hi, I'm trying to parse a WSDL string and I keep getting a SAXException. This is where the processing stops:

Document doc = docBuilder.parse(new InputSource(new StringReader(wsdl)));

'wsdl' is the string I'm trying to parse. I have no experience at all in Java programming and I've been stuck on this for ages. Does anyone have a clue?

Thanks in advance!
  • 0

#2 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 28 July 2012 - 01:32 PM

Could you post a small complete program that compiles, executes and shows the problem?
  • 0

#3 tucano

tucano

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 28 July 2012 - 02:29 PM

It's actually a JSP page, Norm. Hope it's not too long. Here it goes (page imports were omitted):

<%

String wsdl = request.getParameter("wsdl");

try {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        dbf.setNamespaceAware(true);

        DocumentBuilder docBuilder = dbf.newDocumentBuilder();

        Document doc = docBuilder.parse(new InputSource(new StringReader(wsdl))); /* This is where it stops */

        NamespaceContext ctx = new NamespaceContext() {

                public String getNamespaceURI(String prefix) {

                        String uri;
                        if (prefix.equals("sml"))
                                uri = "http://www.opengis.net/sos/1.0";

                        else if (prefix.equals("gml"))
                                uri = "http://www.opengis.net/sos/1.0";

                        else if (prefix.equals("swe"))
                                uri = "http://www.opengis.net/sos/1.0";

                        else
                                uri = null;

                        return uri;
                }

                public Iterator<int[]> getPrefixes(String val) {
                        return null;
                }

                public String getPrefix(String uri) {
                        return null;
                }
        };

        XPathFactory factory = XPathFactory.newInstance();

        XPath xpath = factory.newXPath();

        xpath.setNamespaceContext(ctx);

        XPathExpression expr = xpath.compile("//sml:SensorML/*/sml:value");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;

        int len = nodes.getLength();

        for (int i = 0; i < len; i++) {

                out.print(nodes.item(i).getNodeValue());
        }

} catch (ParserConfigurationException e) {

        e.printStackTrace();

} catch (SAXException e) { /* This is the exception I get */

        e.printStackTrace();

} catch (IOException e) {

        e.printStackTrace();

} catch (XPathExpressionException e) {

        e.printStackTrace();
}
%>


Thanks for replying!
  • 0

#4 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 28 July 2012 - 02:37 PM

Sorry, I can't compile and execute a jsp page for testing. Can you make it a source that would compile and execute?
  • 0

#5 tucano

tucano

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 28 July 2012 - 02:56 PM

Sure, I didn't get it at first. Sorry for it. Here it is:

I tried to run it but got something else: now it says the prefix "sml" is not bound, and another exception showed up: SAXParseException. Is something wrong with the XML shema or something?

import org.w3c.dom.NodeList;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathException;
import java.io.IOException;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import java.io.StringReader;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class test {

public static void main (String args[]) {
	
	 String wsdl = "<sml:SensorML version='1.0.1' xsi:schemaLocation='http://www.opengis.net/sensorML/1.0.1 http://schemas.opengis.net/sensorML/1.0.1/sensorML.xsd'><sml:member><sml:System><!--sml:identification element must contain the ID of the sensor--><sml:identification><sml:IdentifierList><sml:identifier><sml:Term definition='urn:ogc:def:identifier:OGC:uniqueID'><sml:value>" +
"urn:ogc:object:feature:Sensor:IFGI:ifgi-sensor-1" +
"</sml:value></sml:Term></sml:identifier></sml:IdentifierList></sml:identification><!-- sml:capabilities element has to contain status and mobility information --><sml:capabilities><swe:SimpleDataRecord><!-- status indicates, whether sensor is collecting data at the moment (true) or not (false) --><swe:field name='status'><swe:Boolean><swe:value>true</swe:value></swe:Boolean></swe:field><!-- status indicates, whether sensor is mobile (true) or fixed (false) --><swe:field name='mobile'><swe:Boolean><swe:value>false</swe:value></swe:Boolean></swe:field></swe:SimpleDataRecord></sml:capabilities><!-- last measured position of sensor --><sml:position name='sensorPosition'><swe:Position referenceFrame='urn:ogc:def:crs:EPSG::4326'><swe:location><swe:Vector gml:id='STATION_LOCATION'><swe:coordinate name='easting'><swe:Quantity axisID='x'><swe:uom code='degree'/><swe:value>7.727958</swe:value></swe:Quantity></swe:coordinate><swe:coordinate name='northing'><swe:Quantity axisID='y'><swe:uom code='degree'/><swe:value>51.883906</swe:value></swe:Quantity></swe:coordinate><swe:coordinate name='altitude'><swe:Quantity axisID='z'><swe:uom code='m'/><swe:value>52.0</swe:value></swe:Quantity></swe:coordinate></swe:Vector></swe:location></swe:Position></sml:position><!-- list containing the input phenomena for this sensor system --><sml:inputs><sml:InputList><sml:input name='waterlevel'><swe:ObservableProperty definition='urn:ogc:def:phenomenon:OGC:1.0.30:waterlevel'/></sml:input></sml:InputList></sml:inputs><!-- list containing the output phenomena of this sensor system; ATTENTION: these phenomena are parsed and inserted into the database; they have to contain offering elements to determine the correct offering for the sensors and measured phenomena --><sml:outputs><sml:OutputList><sml:output name='waterlevel'><swe:Quantity definition='urn:ogc:def:phenomenon:OGC:1.0.30:waterlevel'><gml:metaDataProperty><offering><id>GAUGE_HEIGHT</id><name>" +
"waterlevel of rivers in North" +
"Rhine Westfalia" +
"</name></offering></gml:metaDataProperty><swe:uom code='cm'/></swe:Quantity></sml:output></sml:OutputList></sml:outputs><!-- description of components of this sensor system; these are currently not used by the 52N SOS --><sml:components><sml:ComponentList><sml:component name='water sensor'><sml:Component><sml:identification><sml:IdentifierList><sml:identifier><sml:Term definition='urn:ogc:def:identifier:OGC:uniqueID'><sml:value>" +
"urn:ogc:object:feature:Sensor:water_level_sensor" +
"</sml:value></sml:Term></sml:identifier></sml:IdentifierList></sml:identification><sml:inputs><sml:InputList><sml:input name='waterlevel'><swe:ObservableProperty definition='urn:ogc:def:phenomenon:OGC:1.0.30:waterlevel'/></sml:input></sml:InputList></sml:inputs><sml:outputs><sml:OutputList><sml:output name='waterlevel'><swe:Quantity definition='urn:ogc:def:phenomenon:OGC:1.0.30:waterlevel'><swe:uom code='cm'/></swe:Quantity></sml:output></sml:OutputList></sml:outputs></sml:Component></sml:component></sml:ComponentList></sml:components></sml:System></sml:member></sml:SensorML>";
					
	 try {

		 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		 dbf.setNamespaceAware(true);

		 DocumentBuilder docBuilder = dbf.newDocumentBuilder();

		 Document doc = docBuilder.parse(new InputSource(new StringReader(wsdl)));

		 NamespaceContext ctx = new NamespaceContext() {

			 public String getNamespaceURI(String prefix) {

				 String uri;
				 if (prefix.equals("sml"))
								 uri = "http://www.opengis.net/sos/1.0";

				 else if (prefix.equals("gml"))
								 uri = "http://www.opengis.net/sos/1.0";

				 else if (prefix.equals("swe"))
								 uri = "http://www.opengis.net/sos/1.0";

				 else
								 uri = null;

				 return uri;
			 }

			 public Iterator<int[]> getPrefixes(String val) {
							 return null;
			 }

			 public String getPrefix(String uri) {
							 return null;
			 }
		 };

		 XPathFactory factory = XPathFactory.newInstance();

		 XPath xpath = factory.newXPath();

		 xpath.setNamespaceContext(ctx);

		 XPathExpression expr = xpath.compile("//sml:SensorML/*/sml:value");

		 Object result = expr.evaluate(doc, XPathConstants.NODESET);

		 NodeList nodes = (NodeList) result;

		 int len = nodes.getLength();

		 for (int i = 0; i < len; i++) {

			 System.out.print(nodes.item(i).getNodeValue());
		 }

} catch (ParserConfigurationException e) {

	 e.printStackTrace();

} catch (SAXException e) {

	 e.printStackTrace();

} catch (IOException e) {

	 e.printStackTrace();

} catch (XPathExpressionException e) {

	 e.printStackTrace();
}

	 System.exit(0);
}
}

  • 0

#6 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 28 July 2012 - 03:08 PM

Sorry again, I don't know XML.
  • 0

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 29 July 2012 - 02:07 AM

It's just invalid xml you're trying to parse because the namespace "sml" isn't declared anywhere.
You'd need
xmlns:sml="something"
...
xsi:schemalocation="... something <urlToXSD>"

For SML, i guess it's:
xmlns:sml="http://www.opengis.net/sensorML/1.0.1"
xsi:schemaLocation="http://www.opengis.net/sensorML/1.0.1 http://schemas.opengis.net/sensorML/1.0.1/sensorML.xsd"
this should be in the root tag, And you seem to be missing the first line of this.
  • 0

#8 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 10 August 2012 - 09:48 AM

If possible can you post the exception error. I just want to see the stack trace.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download