JDOM, SAX, InputStream problems

JaZziD
JaZziD used Ask the Experts™
on
Hi guys,

I have this problem, which i dont know how to solve it. I hope you guys can help me with this.

Im creating a program, which basically produce an XML file and then send it to somewhere using URL/HttpURLConnection as a string. (This one works fine)

Now, i have problems when i tried to handle the response back (which is also a string with XML tags in it)

Im using JDOM to parse the XML

Here are the problems:


***** PROBLEM 1 ***********

If i tried to build the Doc by using the response as a string i will get error message saying :

JDOMException: File "xxxxxxxx" not found
(xxxx is actually all of those XML tags).

This is why i dont get it. I pass it as a string, why it's reading it as a File name?

here's the Java code to read the XML:

public XMLParser(String r)
{
  try
  {
     SAXBuilder builder = new SAXBuilder();
       doc = builder.build(r);
       root = doc.getRootElement();  
       .....
       ...
       ..
       
       // do something with the elements
  }
  catch(Exception e)
  {
     System.out.println("Error: "+e);
  }
}

Here's the code to pass the response from the server:

public void sendXML(String my_first_xml)
{
   StringBuffer response = new StringBuffer();
   try
   {
     URL url = new URL("http://www.xxx.com:80/testxml.php3");
     HttpURLConnection huc = (HttpURLConnection)url.openConnection();
     huc.setRequestMethod("POST");
     huc.setRequestProperty("Host", url.getHost());
     huc.setDoOutput(true);
     PrintWriter writer = new PrintWriter(huc.getOutputStream());
     writer.println("my_first_xml="+java.net.URLEncoder.encode(my_first_xml));
     writer.flush();
     writer.close();

       BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
     String line;
     while((line = br.readLine()) != null)
       {
         response.append(line);
         response.append("\n");
       }
    }
    catch(Exception e)
    {
       System.out.println("error ="+e);
    }

    String r = response.toString().trim();
    XMLParser xmlresponse = new XMLParser(r);
}

******** PROBLEM 2**************

If i tried to use InputStream to build the Document i got a different error message:

JDOMException: The XML declaration may only appear at the very beginning of document

here's the Java code to read the XML:

public XMLParser(InputStream r)
{
  try
  {
     SAXBuilder builder = new SAXBuilder();
       doc = builder.build(r);
       root = doc.getRootElement();  
       .....
       ...
       ..
       
       // do something with the elements
  }
  catch(Exception e)
  {
     System.out.println("Error: "+e);
  }
}

Here's the code to pass the response from the server:

public void sendXML(String my_first_xml)
{
   StringBuffer response = new StringBuffer();
   InputStream in = null;

   try
   {
     URL url = new URL("http://www.xxx.com:80/testxml.php3");
     HttpURLConnection huc = (HttpURLConnection)url.openConnection();
     huc.setRequestMethod("POST");
     huc.setRequestProperty("Host", url.getHost());
     huc.setDoOutput(true);
     PrintWriter writer = new PrintWriter(huc.getOutputStream());
     writer.println("my_first_xml="+java.net.URLEncoder.encode(my_first_xml));
     writer.flush();
     writer.close();

       in = huc.getInputStream();
    }
    catch(Exception e)
    {
       System.out.println("error ="+e);
    }

    XMLParser xmlresponse = new XMLParser(in);
}

***********

That's my problem -- I hope it's all make sense what i wrote above

im sooo confused with this, since this is the first time for me to do this kinda project.

Can somebody please help me with this?

Thank you so much!
Andre
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018
Commented:
> JDOMException: The XML declaration may only appear at the very beginning of document

  Are you sure that your xml file does not have anything above the <?xml version="1.0" ?> declaration line? This line should be the first line in the file otherwise the file you ar trying to parse will not be a valid XML file.
Top Expert 2016

Commented:
Can you show us the exact xml output of your script?

Author

Commented:
Actually guys....

I just found the problems. Apparentely my colleague did a silly mistake in his PHP file.

He was echoing the response twice!

So that's why i got that JDOMException:The XML declaration may only appear at the very beginning of document.

Cause there was another <?xml version="1.0" ?> at the bottom!

Cheers!
Andre
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Top Expert 2016

Commented:
OK. Can you ask CS to delete the question?

Author

Commented:
hmmmm who's CS? just wondering...
Distinguished Expert 2018

Commented:
 Community Support :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial