• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java & XML Parsing XML Response String

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Error is: java.net.MalformedURLException: no protocol: <?xml version="1.0"?>

Code used is:
try {
factory = SAXParserFactory.newInstance();
saxParser = factory.newSAXParser();
} catch (Exception e) {
System.out.println("In ShipmentTrackingAction.execute - " + e);
}
try {
handler = new DefaultHandler() {
boolean Shipment_ = false;
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("Shipment")) {
Shipment_ = true;
}
}

public void characters(char ch[], int start, int length) throws SAXException {
if (Shipment_) {
System.out.println("Shipment: ");
parsedResponse.append("Shipment: ");
parsedResponse.append("<br>");
Shipment_ = false;
}
}
};
saxParser.parse(dhlResponse, handler);
} catch (Exception e) {
parsedResponse.append(dhlResponse);
System.out.println("In ShipmentTrackingAction.dhlParser - " + e);
e.printStackTrace();
}
//return parsedResponse.toString();
return parsedResponse.toString();
}


XML trying to parse starts like this:
<?xml version="1.0"?>
<ECommerce version="1.1" action="Response" timestamp="2005/08/18T12:35:54" transmission_reference="E14D4E8C">


Stack trace is:
java.net.URL.<init>(URL.java(Compiled Code))
java.net.URL.<init>(URL.java(Inlined Compiled Code))
java.net.URL.<init>(URL.java(Inlined Compiled Code))
org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
javax.xml.parsers.SAXParser.parse(Unknown Source)
javax.xml.parsers.SAXParser.parse(Unknown Source)
com.xse.utility.XmlTransmitterSUN.dhlParser(XmlTransmitterSUN.java:693)
com.xse.controller.actions.ShipmentTrackingAction.execute(ShipmentTrackingAction.java:73)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:974)
com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:555)
com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:200)
com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:119)
com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:618)
com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:593)
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As dhlResponse is a string (I'm assuming, since you are appending it to parsedResponse which I guess is a StringBuffer), then you are invoking the parse method on SAXParser with the signature:

from the JavaDoc for SAXParser

As you can see, the first parameter is a URI, not an XML string. So the parser is trying to open your XML string as a URI, and thus the exception.
You will need to use an InputSource as the first parameter of the parse method.

Use a ByteArrayInputStream to get your String into InputSource format.
[ August 18, 2005: Message edited by: DW Bolton ]
 
Ed Berrey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
Was exactly what was needed.
Code now works as designed.
Again, thank you!
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic