Include Schema Name When Creating XML

Discussion in 'Java' started by dmckeon@ameritas.com, Apr 26, 2006.

  1. Guest

    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?

    package igxml;

    import java.io.*;
    import java.util.Vector;

    import org.w3c.dom.*;
    import org.xml.sax.helpers.DefaultHandler;
    import javax.xml.parsers.*;
    import org.apache.xml.serialize.*;

    public class IgXMLBuild extends DefaultHandler {

    Document doc;
    Element root;
    Element currentElement;
    Element previousElement;
    Element childElement;
    int elementIndex;
    Text textData;
    private Vector elementList = new Vector();

    /**
    * This method is used to create a new XML document (DOM object)
    * @param rootName This is the name of the Root element.
    */
    public void startDocument(String rootName)
    {

    // Create the DOM object
    DocumentBuilderFactory factory =
    DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
    builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
    System.out.println("Could not locate a JAXP DocumentBuilder
    class");
    }
    doc = builder.newDocument();

    /* create the root node */
    root = doc.createElement(rootName);

    /* Initialiaze the vector that will hold the element pointers. */
    elementList.clear();
    elementList.addElement(root);
    currentElement = root;
    return;
    }

    /**
    * This method will create the open tag for a new element.
    * @param elementName The name of the new element.
    */
    public void startTag (String elementName)
    {
    /* create a row node */
    currentElement = doc.createElement(elementName);

    /* add Element ID to Vector */
    elementList.addElement(currentElement);
    return;
    }

    /**
    * This method will create the end tag for the last open tag
    (startTag) that was created.
    * @param elementName This parameter is not used in the function. It
    is for documentation purposes only.
    */
    public void endTag(String elementName)
    {
    /* append the row node to the parent node */
    elementIndex = elementList.indexOf(currentElement);
    elementIndex += -1;
    previousElement = (Element)elementList.get(elementIndex);
    previousElement.appendChild(currentElement);
    elementIndex += 1;
    elementList.remove(elementIndex);
    currentElement = previousElement;
    }

    /**
    * This method is used to create an XML Open/Close tag with data
    * @param elementName XML Tag Name for open/close tag
    * @param elementValue Data between open/close tag
    */
    public void addTag(String elementName, String elementValue)
    {
    childElement = doc.createElement(elementName);
    textData = doc.createTextNode(elementValue);
    childElement.appendChild(textData);
    currentElement.appendChild(childElement);
    }

    /**
    * This method is used to create all of the close tags for
    * any tags that were opened (startTag) and not yet closed.
    */
    public void closeTags(){
    while (root != currentElement) {
    endTag("");
    currentElement = (Element)elementList.lastElement();
    }
    }

    /**
    * This method is called when you are done building the XML document.
    * It will close any tags that were not yet closed.
    * @return The XML document will be returned as a String.
    */
    public String finishDocument()
    {
    closeTags();

    /* append the root node to the document */
    doc.appendChild(root);

    try {
    // Serialize the document
    StringWriter out;
    out = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(out, new
    OutputFormat(doc));
    serializer.serialize(doc);
    String xmlString = out.toString();
    return xmlString;
    } catch (IOException e) {
    System.err.println(e);
    return e.toString();
    }
    }
    }
    , Apr 26, 2006
    #1
    1. Advertising

  2. Guest

    Bump
    , Apr 26, 2006
    #2
    1. Advertising

  3. wrote:
    > 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.

    --
    John Bollinger
    John C. Bollinger, May 2, 2006
    #3
  4. Guest

    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.
    , May 2, 2006
    #4
  5. Guest

    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?
    , May 3, 2006
    #5
  6. Guest

    I was able to do it using setAttribute rather than setAttributeNS.
    , May 4, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,530
    Markus
    Nov 23, 2005
  2. Fred Smith
    Replies:
    1
    Views:
    943
    Martin Honnen
    Feb 5, 2004
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,251
    Stanimir Stamenkov
    Apr 25, 2005
  4. bala
    Replies:
    1
    Views:
    264
    Anand Pillai
    Feb 3, 2004
  5. Replies:
    3
    Views:
    2,835
Loading...

Share This Page