You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Dennis Sosnoski <dm...@sosnoski.com> on 2001/10/03 04:14:30 UTC
DOM programming question
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: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: DOM programming question
Posted by John Snelson <jp...@decisionsoft.com>.
Your code is correct, and will set the namespace and prefix. I am
slightley worried, however, that you say your element, when output, was
in the wrong namespace. I suspect a bug in the namespace normalization
code, if you are correct.
I am mainly a xerces-c user, but does anyone know the state of the
normalization code in xerces-j?
Dennis Sosnoski wrote:
>
> 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
--
John Snelson, Software Engineer DecisionSoft Ltd.
Telephone: +44-1865-203192 http://www.decisionsoft.com
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: DOM programming question
Posted by Dennis Sosnoski <dm...@sosnoski.com>.
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: xerces-j-user-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
> --
> John Snelson, Software Engineer DecisionSoft Ltd.
> Telephone: +44-1865-203192 http://www.decisionsoft.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: DOM programming question
Posted by John Snelson <jp...@decisionsoft.com>.
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: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
--
John Snelson, Software Engineer DecisionSoft Ltd.
Telephone: +44-1865-203192 http://www.decisionsoft.com
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org