Search code examples
xmldocumentwell-formed

how to convert xml to document in java with escape single tags


there is some tags such <img> that their end-tag is optional.

String xml = "<div><img href=""></div>";

and not converted to document object through below code

DocumentBuilderFactory factory =
DocumentBuilderFactory.    DocumentBuilder builder;
builder = factory.newDocumentBuilder();  
Document doc = builder.parse(xml); 

the error is

The markup in the document preceding the root element must be well-formed.


Solution

  • The error message means exactly what it says. End tags in XML are not optional; your input is therefore not well-formed XML, and therefore the parser rejects it.