Here's on example (compiled against JDK 1.6) of how you would create
that element as the root element via the DOM APIs. Essentially, it
boils down to use the 'createXXXNS' and 'setXXXNS' methods.

-Nathan

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DOMWithNS {

        public static void main(String[] args) throws Exception {
                DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
                factory.setNamespaceAware(true);
                DocumentBuilder builder = factory.newDocumentBuilder();

                // Create the Document
                Document doc = builder.newDocument();
                // Create the root element with a namespace
                Element pid = doc.createElementNS("urn:hl7-org:v2xml", 
"hl7:PID");
                // add the root element to the Document
                doc.appendChild(pid);
                // add the xmlns:hl7 attribute to this element
                pid.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, 
"xmlns:hl7",
                                "urn:hl7-org:v2xml");
                // add the xmlns:xsi attribute to this element
                pid.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, 
"xmlns:xsi",
                                XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
                // add the xsi:schemaLocation attribute
                pid.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
                                "xsi:schemaLocation", "urn:hl7-org:v2xml 
segments_test.xsd");

                // identity transform to print document to standard out
                TransformerFactory.newInstance().newTransformer().transform(
                                new DOMSource(doc), new 
StreamResult(System.out));
        }

}



On 8/31/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> Is there a way I could specify that my document comes from
> multiple
> namespaces. There seems to be only one namespace parameter.
>
> To be clear, i want to write the following thing:
>
> <hl7:PID xsi:
> schemaLocation="urn:hl7-org:v2xml segments_test.xsd" xmlns:xsi="http:
> //www.w3.org/2001/XMLSchema-instance" xmlns:hl7="urn:hl7-org:v2xml">
>
> This is the piece of code where i create and fill the Document:
>
>
> public Document encodeDocument(Message source) throws HL7Exception {
>         String messageClassName = source.getClass().getName();
>
> String messageName = messageClassName.substring(messageClassName.
> lastIndexOf('.') + 1);
>         org.w3c.dom.Document doc = null;
>
> try {
>                 DocumentBuilderFactory dbf = DocumentBuilderFactory.
> newInstance();
>                 dbf.setNamespaceAware(true);
>                 dbf.
> setAttribute(JAXPConstants.JAXP_SCHEMA_SOURCE, "urn:hl7-org:v2xml
> segments_test.xsd");
>             doc = dbf.newDocumentBuilder().
> newDocument();
>             Element root = doc.createElement
> (messageName);
>             doc.appendChild(root);
>         }
>
> How to
> specify here the information regarding the namespaces?
>
> Greetz,
> Fabio
> Daprile
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to