2

In case the provider supplies a XML string that does not parse due to parse errors (and thet will not fix this for a while), I was wondering if it is possible to perform some validations to detect and correct the XML so it will be fail-proof.

Some examples of issues can be:

The rule of thumb is to get <> replaced for &lt;&gt;

  • Lonely < replaced for &lt;
  • Words like <this> that are not XML tags (the criteria can be to replace the < and > symbols to ignore the unclosed tag.
  • Math formulas like this: 5<x<10

I can't come with more scenarios at the moment, and I think I detected one o those with regex, but that is not enought.

I would like to read your comments.

2
  • This isn't a chatroom. You have used a regex, can you show it? Why isn't it enough ? And why is the XML broken in the first place ? Could you show some sample input and expected output ?
    – HamZa
    Nov 28, 2013 at 23:48
  • @HamZa I've used regex to solve the words like <this> but it cannot be used for all the cases. I know that this is not a chat room, but I already read some interesting answers and criteria, that was my intentions and sorry for let the disscussion open and not the way you liked.
    – po5i
    Nov 29, 2013 at 16:58

2 Answers 2

1

I was wondering if it is possible to perform some validations to detect and correct the XML so it will be fail-proof.

Your noble intentions are unfortunately misguided. In a fundamental sense, communication mistakes cannot be repaired without relying upon some portion of the protocol being mistake-free.

You can only be so liberal in what you accept. Even Postel's Law has its limits.

Standard practice in building XML-based systems is to require that messages be well-formed XML. (In fact, non-well-formed XML is not XML; see Michael Kay's answer.) Especially when you cannot trust your sender to follow protocol, you should check your input. One of the benefits of XML is that there exist battle-tested parsers to use to perform these checks.

Pull the message off the wire and immediately parse it using a known-reliable parser such as Xerces2. If there are errors, pass them back to the sender to repair and do not attempt to process the message further. If you have a schema, the parse should be conducted with validation turned on against the schema to detect higher-level errors in the protocol as well.

Do not be tempted by the possibility of correcting "obvious" errors in an ad hoc manner. The problem is theoretically unsolvable in the general case, and attempts at applying piecemeal corrections will actually make your system less robust, not more.

0
0

I would recommend using XML for data interchange. It's a great format. When people use XML, you have a wide choice of off-the-shelf parsers available which guarantee that everyone can read your data. By contrast, if you use home-brew formats that are not standardised and not documented then deciphering the data becomes a nightmare.

I would also recommend that if you are using a home-brew format for data interchange, you don't call it XML, because you will only confuse people.

If you want help here in how to parse a home-brew non-XML data interchange format, please don't tag the question as "XML", because you reach the wrong audience. And please provide a specification of the format. I know you don't have one, but writing a program to read data in an unspecified format is not something any competent programmer should ever attempt.

1
  • If it has <> in it then it's not XML. Dec 3, 2013 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.