Using DOM Library to Add Namespace Declarations to Root Element Tag

E

eric.jester

Hello All,

I am trying to modify the root tag of a Document. Specifically, the
root tag is currently <FDXGroundCallTagRequest> and I am trying to
append some namespace information to it so it becomes:

<FDXGroundCallTagRequest xmlns:api="http://www.fedex.com/fsmapi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FDXGroundCallTagRequest.xsd">

I have not been able to figure out how to accomplish this using the DOM
Library. Can someone shed some light on this for me?

Thanks!

Eric
 
D

Daniel Bornkessel

Not all DOM implementations support namespaces.
Anyways, make your life easier and use JDOM (jdom.org), which is a much
simpler solution for Java (much more intuitive).
With this it would be:

// build your JDOM document (reads DOMs and XML files ...)
....
Element root = jdomDocument.getRootElement();
root.setNamespace(Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
root.setNamespace(Namespace.getNamespace("noNamespaceSchemaLocation","FDXGroundCallTagRequest.xsd");
root.setNamespace(Namespace.getNamespace("api","http://www.fedex.com/fsmapi");

// if you need a DOM document
DOMOutputter do = new DOMOutputter ();
org.w3c.dom.Document doc = do.output( jdomDocument );

JDOM can as well read-in and output SAX and XML files,read and write DOCTYPE
definitions (which is not supported by some SAX and DOM parses neither) and
hence saves you a lot of unnecessary work.
 
E

eric.jester

Thanks for you response. I might be under some restrictions when it
comes to introducing new libraries. Are you saying that the
functionality you spoke of is not available in the standard DOM
packaged with the java sdk?

Thanks,

Eric
 
E

eric.jester

Thanks again for the response. I tried implementing it, but when I log
out the doc that is created, the namespaces still do not appear. I
tried adding jdomDocument.setRootElement(root) at the end and that did
not help. Is it possible the namespaces are lost when its converted
back to a w3c document?

Thanks,

Eric
 
E

eric.jester

I figured out that the DOC is being modified (I had a error in the
logging mechanism), however the namespaces are not coming across
correctly. Instead of getting:

<?xml version="1.0" encoding="UTF-8" ?>
<FDXGroundCallTagRequest xmlns:api="http://www.fedex.com/fsmapi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FDXGroundCallTagRequest.xsd">


I am seeing:

<api:FDXGroundCallTagRequest xmlns:api="http://www.fedex.com/fsmapi">
<RequestHeader>

Any ideas?

Thanks Again!

Eric
 
E

eric.jester

I managed to get closer to what I need by using the
addNamespaceDeclaration method instead of the setNamespace method. The
only problem now is that
root.addNamespaceDeclaration(Namespace.getNamespace("noNamespaceSchemaLocation","FDXGroundCallTagRequest.xsd");

generates xmlns:noNamespaceSchemaLocation and I need it to generate
xmi:noNamespaceSchemaLocation.

I can see the light at the end of the tunnel now. Anyone know how to
get there?

Thanks!

Eric
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top