[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Insertion using DOM

  • From: "Narayan, Shashank" <snarayan@e...>
  • To: xml-dev@x...
  • Date: Fri, 12 May 2000 14:14:45 -0400

dom insertchild
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@x...&BODY=unsubscribe%20xml-dev
List archives are available at http://xml.org/archives/xml-dev/
***************************************************************************

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.