0

I'm using dom4j to parse XML files on line.

File file = new File("text.xml");
SAXReader reader = new SAXReader();
Document document = reader.read(file);

There is a syntax error in some XML files, which make the program throw SAXParseException: The prefix X for element X:A is not bound at line reader.read(file).

I know what the error is, and how to make the XML files right. Just as what said in this article

But the problem is the XML file is uploaded by users, I can not change the file before parse it, and I can not ask the user to change the XML file.

So, is there a way to parse the xml file with a undefined prefix error exist?

  • This is a duplicate of https://stackoverflow.com/questions/13593493/ but that also doesn't have an answer. – OrangeDog Sep 23 '22 at 15:23
  • I do not think these two questions are the same. That question is about writing somethin to xml file, this question is about reading a wrong format xml file. – Tachiguishi Sep 24 '22 at 00:55
  • They are both using `SAXReader`, just constructing the source differently. – OrangeDog Sep 24 '22 at 19:20
  • When I am writing a xml, then I know what I the prefix is. But when I read a xml, I can not not what the prefix is, because it throw the Exception when i am reading. – Tachiguishi Sep 25 '22 at 01:14

1 Answers1

0

You could parse it with a DOM parser that isn't Namespace aware, but it will probably open up more problem areas than what you already have. The correct behavior is of course to validate the uploaded files and reject those that isn't correct XML or in any other way violate the contracts in place.

You do have the contracts in place, right?

forty-two
  • 12,204
  • 2
  • 26
  • 36