Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

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

Bill Woodward

2003-12-30

Replies:

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@(protected))  \
  \     http://www.saifa.net       /
   \------------------------------------/
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.