1

I am parsing an XML file to java, there is a wrong starting tag in the file. When I run the Code it shows the following error: [Fatal Error] malformed_routes5.xml:9:26: The element type "WrongTag" must be terminated by the matching end-tag "".

    <Route>
    <FlightNumber>1848</FlightNumber>
    <DayOfWeek>Tue</DayOfWeek>
    <DepartureTime>13:40</DepartureTime>
    <DepartureAirport>Belfast</DepartureAirport>
    <DepartureAirportCode>BFS</DepartureAirportCode>
    <WrongTag>18:05</ArrivalTime>
    <ArrivalAirport>Tenerife-Sur</ArrivalAirport>
    <ArrivalAirportCode>TFS</ArrivalAirportCode>
    <Duration>PT4H25M</Duration>
</Route>

this is the file which contains the wrong tag, do i need to add a specific exception in the catch for this kind of error?

        catch (ParserConfigurationException | SAXException | IOException | IllegalArgumentException ioe) {


        throw new DataLoadingException(ioe);
        //** check the fatal error " The element type "WrongTag" must be terminated by the matching end-tag "</WrongTag>". "
        // ************************************************

    }
Khaled.M
  • 21
  • 1
  • 4
  • Do you have control over the XML? Can you fix that tag? – Michael McKay Dec 26 '19 at 14:28
  • I don't, My parser load multiple XML files, one of them has the wrong tag, I think i have to add an exception or something like that @MichaelMcKay – Khaled.M Dec 26 '19 at 14:34
  • Looks like a `SAXParseException` but you have `SAXException` in the list. It is a possibility that some other class wraps that exception in a Runtime Exception and throws away. Just 2 cents. – The_Cute_Hedgehog Dec 26 '19 at 14:37
  • Is it just this one tag that is the issue? If so, fix the XML before you parse it. A bad tag is difficult to fix in the general case. It could be a mistake made in forming the xml but it could also indicate a chunk of the file is missing. – Michael McKay Dec 26 '19 at 14:37
  • XML parsers are designed to tell you when the XML is broken; the only thing you can do with broken XML is to fix it. I'm not sure what else you are looking for. – Michael Kay Dec 26 '19 at 19:34
  • Q: Why the fatal error? A: Because the XML is broken. See duplicate link for how to parse bad XML. If your real Q is how to recover from a fatal error, the answer is that you don't -- that's what ***fatal*** means. Best you can do is catch the exception, inform caller, and move on -- basic Java programming. – kjhughes Dec 27 '19 at 13:19

2 Answers2

0

Looks like your XML is broken. I would catch the exception and send it back to the client OR if I own the code that generates this XML, fix that part of the code.

JAVA is throwing a legit error where <WrongTag> isn't being closed by </WrongTag> but instead by <ArrivalTime>.

Samarth
  • 431
  • 1
  • 5
  • 13
  • I already know that the file is broken, I am trying to get rid of this error by throwing an exception or something like that, I can't change the XML. – Khaled.M Dec 26 '19 at 15:15
  • 1
    I guess the question is what would you want the calling code to do in this case? Throw a runtime exception for the user and terminate? Or handle it gracefully saying that the program tried to fix this but failed because one of the tags is broken. – Samarth Dec 26 '19 at 15:23
0

There is an error at tag:

<Route>
    <FlightNumber>1848</FlightNumber>
    <DayOfWeek>Tue</DayOfWeek>
    <DepartureTime>13:40</DepartureTime>
    <DepartureAirport>Belfast</DepartureAirport>
    <DepartureAirportCode>BFS</DepartureAirportCode>
    <WrongTag>18:05**</WrongTag>**
    <ArrivalAirport>Tenerife-Sur</ArrivalAirport>
    <ArrivalAirportCode>TFS</ArrivalAirportCode>
    <Duration>PT4H25M</Duration>
</Route>