3

Let's say there were errors in an XML message:

Well-Formed

<Person><Name>Attila</Name><ID>001</ID><Age>45</Age></Person>

Not Well-Formed

<Pxxxon><Name>Attila</9327><ID>001</ID><Age>45</Age></Person>

Are there any Java libraries or code to format the non-well formed XML message to:

<Pxxxon>
    <Name>Attila</9327>
    <ID>001</ID>
    <Age>45</Age>
</Person>

I understand that current Java libraries only format valid XML messages to this prettified format.

bouncingHippo
  • 5,940
  • 21
  • 67
  • 107
  • Your example is simple, but still, that would need an AI to determine that Pxxon correspond to Person. It could be that Person is missing a Person node, or that Pxxxon is missing a closing, so which of the 3 is right ? Imagine if there is multiple errors, that will lead to exponential output possibilities – flafoux May 13 '15 at 14:09

1 Answers1

2

No, because what you list as "Invalid" is actually not well-formed.

Well-formed and valid are not the same thing.

  • Well-formed means that a textual object meets the W3C requirements for being XML.
  • Valid means that well-formed XML meets additional requirements given by a specified schema.

See Well-formed vs Valid XML for more details, but if data is not well-formed, it is not XML at all and no XML parser will be able to read it to reformat it.

You might then ask what about non-XML parsers? To which we would reply, if it's not XML, what format is it? For any parser to be able to read any data, the syntax of the data has to be defined. Simply saying that the data resembles XML insufficiently specifies the format, and that is why you'll not find a tool that can pretty-print the data sample you provided.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240