[jdom-interest] what happens when a duplicate element is added

Bill Woodward wpwood at saifa.net
Tue Dec 30 11:52:26 PST 2003


William Krick said:
>
> Using JDom b9, and given the following xml...
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>   <tag1>ABC</tag1>
>   <tag2>123</tag2>
> </root>
>
>
> ...assume I have the above XML in a valid Document object "doc"
> and I do this in my code...
>
> Element root = doc.getRootElement();
> root.addContent(new Element("tag2").addContent("456"));
>
> What happens?
>

I would expect you to get:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <tag1>ABC</tag1>
  <tag2>123</tag2>
  <tag2>456</tag2>
</root>

This is perfectly good, well-formed XML.  There's no reason that you can't
have duplicate tags in a well-formed XML document.

On the other hand, depending on whether you validate it, it might not be
*valid*, but that's dependent on the DTD/XSD that you're validating
against.  Since JDOM does not check validity against a DTD/XSD after the
JDOM tree is created, the addContent should work just fine.

- Bill

      /------------------------------------\
     /   Bill Woodward (wpwood at saifa.net)   \
     \        http://www.saifa.net          /
      \------------------------------------/



More information about the jdom-interest mailing list