Thanks for the response, John. However, if I don't set the prefix the added
element
just shows up in the default namespace when the document is output as text. Am I
missing something here?
The original code generated the correct XML output document text for all the
cases I
tried, but I didn't look at the internal state (only the output). I guess what I
really should do to make this correct is use the prefix from the parent element
to
construct the name of the new element (if the prefix is not null), then always
use
createElementNS to create the new element:
String name = (prefix == null) ? "text" : (prefix + ":text");
Element text = doc.createElementNS(element.getNamespaceURI(), name);
Any problem with that?
Thanks again,
- Dennis
John Snelson wrote:
> You can have an element whose prefix is null, but is still in a
> namespace. Why not try this?
>
> Element text =
> element.getOwnerDocument().createElementNS(element.getNamespaceURI(),
> "text");
>
> This should work in all cases, but please note that it does not set the
> prefix of the "text" element.
>
> John
>
> Dennis Sosnoski wrote:
> >
> > I've got a question about DOM programming I wanted to check for an upcoming
> > article on using Java document models. It's not strictly XercesJ, but
> > that's the
> > implementation I'm using.
> >
> > The code needs to create a new element ("text") with the same namespace as
> > an
> > existing element ("element"). Here's the code I came up with:
> >
> > Document doc = element.getOwnerDocument();
> > String prefix = element.getPrefix();
> > String uri = element.getNamespaceURI();
> > Element text;
> > if (prefix == null) {
> > text = doc.createElement("text");
> > } else {
> > text = doc.createElementNS(element.getNamespaceURI(),
> > prefix + ":text");
> > }
> >
> > Is there a simpler way to do this that I'm missing?
> >
> > Thanks for any pointers,
> >
> > - Dennis
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
> --
> John Snelson, Software Engineer DecisionSoft Ltd.
> Telephone: +44-1865-203192 http://www.decisionsoft.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]