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]

Reply via email to