Forum Stats

  • 3,876,265 Users
  • 2,267,083 Discussions
  • 7,912,487 Comments

Discussions

How to Read/Parse XML who's dynamically generated

843834
843834 Member Posts: 49,972
edited Jul 14, 2001 6:28AM in Java Technology & XML
Web Site #1 - http://sde/xmlstore

I have a servlet (contactServlet) running that generates XML

Web Site #1

I need to read/parse xml generated by site #1 by passing the url http://sde/xmlstore/contactServlet?id=322

How do I do this I've seen a lot of xml read/parse but input as files.

Thanks in Advance

Comments

  • 843834
    843834 Member Posts: 49,972
    try{

    URL urn = new URL(url);//where url in braces is the actual url for resource to be read
    BufferedReader br = new BufferedReader(new InputStreamReader(urn.openStream()));

    String str="";

    while((str=br.readLine())!=null){
    //process information
    }
    } catch (MalformedURLException e) {
    System.err.println("Error Url :"+e);
    }catch (IOException e) { // openConnection() failed
    System.err.println("Error:"+e);
    }
    as long as you know what you want to do with xml,it should essentially work.
    Khalid
  • 843834
    843834 Member Posts: 49,972
    Hi,

    I need to parse a String object containing XML - I've tried several ways but (as stated in previous thread) the parser only accepts InputStreams or URL/Files.

    How do I convert the String object into an InputStream? Or better yet, how do I parse the String object directly? (if possible)

    Thanks and regards,
    Arik.
  • 843834
    843834 Member Posts: 49,972
    Hei!
    I have a similar problem, slightliy different.
    I convert the xml string to inputStream using StringBufferInputStream and send it to the parse method.
    It works fine until I add the DTD header in the xml string, the parser can not find the file containing the DTD(myDtd.dtd) in the xml string.(even with the full path)

    String xmlMsg=
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
    "<!DOCTYPE elm1 SYSTEM \"myDtd.dtd\">"+
    "<elm1>"
    "<elm2>" +
    "something" +
    "</elm2>"+
    "</elm1>";

    StringBufferInputStream sbis= new StringBufferInputStream(xmlMsg);

    saxParser.parse( sbis, this);


    appreciate any help


  • 843834
    843834 Member Posts: 49,972
    Isnt' the StringBufferInputStream class deprecated? The Java API documentation says it doesn't translate characters properly.

    Java pros - how do you parse a String object ???
  • 843834
    843834 Member Posts: 49,972
    try{


    String s = new String("Any String object here");
    BufferedReader br = new BufferedReader(new StringReader(s));
    String str="";

    while((str=br.readLine())!=null){
    //process information
    System.err.println(str);
    }

    }catch (IOException e) { // openConnection() failed
    System.err.println("Error:"+e);
    }
  • 843834
    843834 Member Posts: 49,972
    Actually, by parsing I meant XML parsing, by using the DOM classes.

    But I have found the solution - I don't know how I missed it but one should simply create an InputSource object, which accepts a StringReader that contains the string.


    String params = "<a>xml contents</a>";
    InputSource inputSource = new InputSource (new StringReader (params));
    Document doc = parser.parse(inputSource);

    System.out.println (doc);
This discussion has been closed.