-1

Here is my code snippet:

public EpsXmlParser(String xmlAnswer) {

   DOMParser parser = new DOMParser();

   try {
          InputSource is = new InputSource();

          is.setCharacterStream(new StringReader(xmlAnswer));

          parser.parse(is);


    } catch (SAXException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    } catch (Exception e) {

        e.printStackTrace();
    }

   Document doc = parser.getDocument();
   parseXmlDoc(doc.getDocumentElement());

}

In this code block String xmlAnswer is actually an UTF-8 encoding xml taken from another machine using web service client program. I realized when debugging that the problem here is after parser.getDocument() method implemented Document doc is being null. I can not fix the problem. Please help me what should I do?


I can not get any exception. Code runs pretty well but Document doc will be like this (look at the snapshot below). I can not understand what is the problem is. Any help will be appreciated.

enter image description here


I used an XML like this. Is this XML format standard? If it is standard how can not I get any exception while using the predefined xml parsing codes.

<?xml version="1.0" encoding="UTF-8"?>
<extra_result><status>00</status><data><transaction_id>3c704f15-7c09-4bba-9046-                   ffbdb8c97b51</transaction_id><card_status>11</card_status><status_msg>Kart numarası yanlış.              </status_msg><card_no>48422</card_no><name_surname></name_surname><gsm></gsm><bonus></bonus></data></extra_result>
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user1112085
  • 29
  • 1
  • 6
  • You haven't told so what error you're getting - and your exception handling is quite possibly the cause here. If the parse part fails, why are you continuing? Is another exception being displayed before the NullPointerException you're presumably seeing due to `doc` being null? – Jon Skeet Feb 08 '14 at 17:05
  • There is no exception that I get or catch at the catch blocks. The XML format is standard but I can not parse it using the pre defined codes. The problem here is actually I can not get an exception but Document is still null.How it can be possible? – user1112085 Feb 08 '14 at 17:50
  • It sounds unlikely to me. It would help if you could provide a short but complete example - both the XML (as short as you can get to give a complete example) and a complete *program*. – Jon Skeet Feb 08 '14 at 17:53
  • Agreed. If you aren't getting an error, the document is probably being parsed correctly and you simply aren't using the result properly. Without knowing more about what's actually failing, though, we can't help you. – keshlam Feb 08 '14 at 18:01

1 Answers1

0

I have managed to run the code with no problem by providing an example XML string document.

It suggests the problem is most likely with the XML string itself given in the xmlAnswer argument.

In order to see where exactly is your problem try to change your code and run the following:

public EpsXmlParser(String xmlAnswer) {

    DOMParser parser = new DOMParser();

    try {
           InputSource is = new InputSource();

           is.setCharacterStream(new StringReader(xmlAnswer));

           parser.parse(is);


     } catch (Exception e) {
         throw new RuntimeException("Error on parsing document", e);
     }

    Document doc = parser.getDocument();
    parseXmlDoc(doc.getDocumentElement());

 }

I expect the exception will be thrown with actual reason of parsing error.

xproph
  • 1,171
  • 11
  • 7