> I have a Java program that creates an XML string and I wanted to pass
> in a schema name to include when the DOM object is created. Here is my
> Java code. The startDocument method automatically adds "<?xml
> version="1.0" encoding="UTF-8"?>". Is there something I can add to
> this method to write out the schema location?
No one seems to have responded to this yet, so I'll have a go. The key
point here is that when building a DOM tree via the DOM API, namespace
prefix and schema location declarations have no special significance;
they are simply attributes like any others. You can use one or more of
the Element.setAttribute(), Element.setAttributeNS(),
Element.setAttributeNode(), and Element.setAttributeNodeNS() methods to
add the appropriate attributes to your document (root) element. In
principle, you could do it at any time before you output the document,
but it is probably best to do it right after you create the document
Element.
Considering that you want to specify a schema, I assume that you are
looking to perform document validation somewhere down the processing
chain. For that to work correctly, you need to get elements' namespaces
right, which will require at least one more attribute a default
namespace declaration or another namespace prefix declaration) on the
document element. You might also want to use Document.createElementNS()
to create the document element in the correct namespace in the first
place. You should also note, however, that DOM is myopic with respect
to XML namespaces. You can probably get away with the plain versions of
Document.createElement() and Element.setASttribute() /
Element.setAttributeNode() if all you intend to do with the resulting
DOM tree is to transform it to text for interchange.

Signature
John Bollinger
jobollin@indiana.edu
dmckeon@ameritas.com - 02 May 2006 14:49 GMT
THANK YOU!!!! I thought I was being ignored. Great advice and I will
certainly take it. I will post back if I run into any problems.
dmckeon@ameritas.com - 03 May 2006 20:17 GMT
Okay. I'm struggling a bit. Here is the desired header for my XML
file:
<tns:ImageData xmlns:tns="http://www.test.com/IgXMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.test.com/IgXMLSchema IgXMLSchema.xsd ">
I have tried various combinations of setAttributeNS and I can't seem to
get it to work. Do you have any sample code?
dmckeon@ameritas.com - 04 May 2006 13:53 GMT
I was able to do it using setAttribute rather than setAttributeNS.