Print prefix early in Xrces-J

P

Philipp

Hello,
I have a DOM which serializes to the following XML. As you see, the
namespace prefixed with "f" is repeated for each object. Is there a way
in Xerces-J to make an unique definition of the prefix in the top-level
element (so as to save room)?

Thanks for answers
Phil

<?xml version="1.0" encoding="UTF-8"?>
<e:test xmlns:e="http://www.example.com/e">
<f:value xmlns:f="http://www.example.com/f">
<f:val>12</f:val>
<f:unit>lux</f:unit>
</f:value>
<f:value xmlns:f="http://www.example.com/f">
<f:val>34.1234</f:val>
<f:unit>ampere</f:unit>
</f:value>
<f:value xmlns:f="http://www.example.com/f">
<f:val>45.0</f:val>
<f:unit>empty_key</f:unit>
</f:value>
</e:test>
 
M

Martin Honnen

Philipp said:
I have a DOM which serializes to the following XML. As you see, the
namespace prefixed with "f" is repeated for each object. Is there a way
in Xerces-J to make an unique definition of the prefix in the top-level
element (so as to save room)?

It might work to create an xmlns:f attribute on the root element e.g.

documentInstance.getDocumentElement().setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:f", "http://www.example.com/f");

That way the serialization code is hopefully smart enough to avoid
duplicating the namespace declaration on the child elements.
 
P

Philipp

Martin said:
It might work to create an xmlns:f attribute on the root element e.g.

documentInstance.getDocumentElement().setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:f", "http://www.example.com/f");

That way the serialization code is hopefully smart enough to avoid
duplicating the namespace declaration on the child elements.

I tried it and unfortunately it's not smart enough :-( It really
duplicates the namespace on each child...
 
M

Martin Honnen

Philipp said:
I tried it and unfortunately it's not smart enough :-( It really
duplicates the namespace on each child...

Are you sure those f:value elements do not have an xmlns:f attribute on
their own? If they have then I don't think the serializer will omit them.
So doing
NodeList values =
documentInstance.getElementsByTagName("http://www.example.com/f", "value");
for (int i = 0, l = values.getLength(); i < l; i++)
{
Element value = (Element)values.getItem(i);
value.removeAttributeNS("http://www.w3.org/2000/xmlns/", "f");
}
might be necessary, depending on how your DOM looks.
 
P

Philipp

Martin said:
Are you sure those f:value elements do not have an xmlns:f attribute on
their own? If they have then I don't think the serializer will omit them.

I typically create them with:
Element node = document.createElementNS("http://www.example.com/f",
"f:value");
root.appendChild(node);

This automatically creates the xmlns:f attributes in the output.
So doing
NodeList values =
documentInstance.getElementsByTagName("http://www.example.com/f", "value");
for (int i = 0, l = values.getLength(); i < l; i++)
{
Element value = (Element)values.getItem(i);
value.removeAttributeNS("http://www.w3.org/2000/xmlns/", "f");
}
might be necessary, depending on how your DOM looks.

Yes, I will try that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top