Click to See Complete Forum and Search --> : XML string


scottjsn
09-05-2008, 01:41 PM
Try to understand the XML string:

String x="<rootTag><p>test1</p><p>test2</p></rootTag>";

Document doc = utils.convertXMLToDoc(x);
NodeList nl = doc.getElementsByTagName("p");
Node nd = nl.item(0);
String result = nd.getFirstChild().getNodeValue();

//result returns "test1"

This is what I wanted. So far so good. Now, I go back to remove the root tag.

String x="<p>test1</p><p>test2</p>";

Re-run the above codes, generating an error.

Why must an XML string contain a root tag to wrap the elements?


Thanks


Scott

Alain COUTHURES
09-06-2008, 02:32 PM
It is perfectly clear that a unique root element is mandatory for XML 1.0.

XML 2.0 does not yet exists but some experts consider that multiple roots for one document might be a good idea...

rpgfan3233
09-08-2008, 07:52 PM
I can see some benefits for having multiple root elements in a single document, but I really don't think that it is necessary. The single root element serves as something that describes the entire document. It is essentially the equivalent of an essay topic in that it gives a quick overview regarding what the document contains.