3

I am trying to parse elements out of a XML document. It works for most of them, but sometimes I may get a fatal error because I am given a badly formatted XML document. I want to ignore those cases when I get those XML documents by throwing an exception for it. The console gives me a SAXParseException, but when I call for it in the catch block, it is unable to. I wanted to do it in the JSP scriplet, but it will never be thrown since the scriplet is not actively searching for the SAXException.

I though the solution is to create a method to throw the exception, but again, it says that the SAXParseException will never be thrown in the try/catch statement. What is happening here?

public String parseElementsByTagNameInXML(Document doc, String tagName) throws SAXParseException
        {
            try{
                return doc.getElementsByTagName(tagName).item(0).getTextContent();
        }
        catch(SAXParseException e){
             System.out.println("Failure on Generating MessageID: " + e);
        }

        catch(NullPointerException e){
            //will not spam the console with nullpointexception errors
        }
        catch(Exception e){
            System.out.println("Failure on Generating MessageID: " + e);
        }
            return "";
        }
dur
  • 15,689
  • 25
  • 79
  • 125
Jayizzle
  • 532
  • 1
  • 6
  • 24
  • That message is telling your that `doc.getElementsByTagName(tagName).item(0).getTextContent()` will *never* throw `SAXParseException`, so don't bother trying to catch it. When you say you have a "badly formatted XML document", do you mean that it's not well-formed XML, and can't be parsed to a DOM? Or do you mean that well-formed XML structure isn't what you expect (i.e., it's not valid according to your schema)? – erickson Feb 04 '16 at 20:34
  • Well, Im doing a basic parser from within Document builder, inputsource, and stringreader set to the XML document. The error that I'm getting says that "[Fatal error] -- The content of elements must consist of well-formed character data or markup" and it says it also threw the SAX exception with the given error... – Jayizzle Feb 04 '16 at 20:44
  • I think I've seen the XML in question. It is just a bunch of other completed XML documents pasted into one XML. Complete docs, that being and end, but have another new xml startign on the next line... I can't simply edit that XML doc, its in a database. But I will probally get others like it. – Jayizzle Feb 04 '16 at 20:45
  • That's not valid XML, and I don't think that the method you show here is ever called; I believe the exception is thrown when trying to create the `Document` object. You need to break the documents apart and pass one at a time to the XML parser in order to successfully create a `Document` object to pass to this method. – erickson Feb 04 '16 at 21:38

1 Answers1

0

Your method define that it will throws SAXParseException but your try catch is catching and consuming it without tossing it back up the line. Instead, I think it's printing it out. Try removing that catch and the exception catch, then if it fail by SAXParseException, it'll throw it to whoever call that method.

pompanoSlayer
  • 163
  • 1
  • 11