OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Insertion using DOM

[ Lists Home | Date Index | Thread Index ]
  • From: "Narayan, Shashank" <snarayan@exchange.stk.com>
  • To: xml-dev@xml.org
  • Date: Fri, 12 May 2000 14:14:45 -0400

Folks:
      I'm assuming that this is the right forum to ask any programming
questions. I already posted the same question for SUN at java.sun.com but
have not received any response from them yet. I'd appreciate any help.
      I'm trying to insert an element into an XML document using the DOM
Parser (JAXP 1.0). And I am having problems - it keeps throwing an exception
with the message "This node isn't allowed there." What am I doing wrong ?
Here's what my code looks like. 

Thanks.

-shashank.


// I am trying to insert the source document into the destination document

    protected void onInsert()
    {
          connectDOMParser dest = new connectDOMParser(urlDest);
          connectDOMParser source = new connectDOMParser(urlSource);
            
          Document sourceDoc = source.getDocument();
          Document destDoc   = dest.getDocument();
                    
          NodeList nodeListSource =
sourceDoc.getElementsByTagName("CONNECT_CMD");
          NodeList nodeListDest   =
destDoc.getElementsByTagName("CONNECT_CMD");
          System.out.println("nodeListSource.item(0).getNodeName() = " +
nodeListSource.item(0).getNodeName());
          System.out.println("nodeListDest.item(0).getNodeName() = " +
nodeListDest.item(0).getNodeName());

          dest.InsertChild(nodeListSource.item(0), nodeListDest.item(0));
    }


// Following is my connectDOMParser class.

public class connectDOMParser
{
    protected Document doc = null;

    public connectDOMParser(String uri)
    {
        ParseFile(uri);
    }

    public Document getDocument()
    {
        return doc;
    }

    public void InsertChild(Node newChild, Node refChild)
    {
        try
        {
            doc.insertBefore(newChild, refChild);
        }
        catch(DOMException e)
        {
            System.out.println("DOMParser : " + e.getMessage());
        }
    }

    public void RemoveChild(Node oldChild)
    {
        try
        {
            doc.removeChild(oldChild);
        }
        catch(Exception e)
        {
            System.out.println("DOMParser : Error on remove !\n");
        }
    }

    protected void ParseFile(String uri)
    {
        try
        {
            DocumentBuilderFactory docBuilderFactory = 
                                     DocumentBuilderFactory.newInstance();
            
            docBuilderFactory.setValidating(true);
            
            DocumentBuilder docBuilder =
docBuilderFactory.newDocumentBuilder();
            
            docBuilder.setErrorHandler (new MyErrorHandler ());
            
            doc = docBuilder.parse(uri);

            doc.getDocumentElement().normalize();

            //System.out.println("Root element of the doc is " +
            //
doc.getDocumentElement().getTagName());
        }
        catch(SAXParseException err)
        {
            System.out.println ("** Parsing error" + ", line " +
err.getLineNumber ()
                                 + ", uri " + err.getSystemId ());
            System.out.println("   " + err.getMessage ());
        }
        catch(Exception e)
        {
        }
    }

    static class MyErrorHandler extends HandlerBase
    {
    // treat validation errors as fatal
    public void error (SAXParseException e)
    throws SAXParseException
    {
        throw e;
    }

        // dump warnings too
    public void warning (SAXParseException err)
    throws SAXParseException
    {
        System.out.println ("** Warning" 
        + ", line " + err.getLineNumber ()
        + ", uri " + err.getSystemId ());
        System.out.println("   " + err.getMessage ());
    }
   }

}

-----------------
And this is how my source.xml looks like:

<?xml version="1.0"?>
<!DOCTYPE CONNECT_CMD SYSTEM "connect.dtd">
<CONNECT_CMD name="sdfvfd">
<NAME>
sdfvfd
</NAME>
<PURPOSE>
hjgh
</PURPOSE>
<FORMAT>
jhgjg
</FORMAT>
<SHORT_DESC>
dfdjhg
</SHORT_DESC>
<VERSION>
ghhjgl
</VERSION>
<DESCRIPTION>
kjhkjl
</DESCRIPTION>
<EXAMPLE>
sdfgs
</EXAMPLE>
<RETURN_MSG>
sdffd
</RETURN_MSG>
</CONNECT_CMD>

----------------------
And this is how my destination doc looks like:

<CONNECT_DOC>
<CONNECT_CMD name="aaa">
<NAME>
aaa
</NAME>
<PURPOSE>
bbb
</PURPOSE>
<FORMAT>
ccc
</FORMAT>
<SHORT_DESC>
dd
</SHORT_DESC>
<VERSION>
eee
</VERSION>
<DESCRIPTION>
fff
</DESCRIPTION>
<EXAMPLE>
ggg
</EXAMPLE>
<RETURN_MSG>
hhh
</RETURN_MSG>
</CONNECT_CMD>
<!--ANother CONNECT_CMD element--!>
<CONNECT_CMD name="iii">
<NAME>
iii
</NAME>
<PURPOSE>
jjj
</PURPOSE>
<FORMAT>
kkk
</FORMAT>
<SHORT_DESC>
lll
</SHORT_DESC>
<VERSION>
mmm
</VERSION>
<DESCRIPTION>
nnn
</DESCRIPTION>
<EXAMPLE>
ooo
</EXAMPLE>
<RETURN_MSG>
ppp
</RETURN_MSG>
</CONNECT_CMD>
</CONNECT_DOC>

***************************************************************************
This is xml-dev, the mailing list for XML developers.
To unsubscribe, mailto:majordomo@xml.org&BODY=unsubscribe%20xml-dev
List archives are available at http://xml.org/archives/xml-dev/
***************************************************************************




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS