Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2008

Tip: Looking for answers? Try searching our database.

Problem with AppendChild

Thread view: 
Groufo - 28 Jan 2008 14:28 GMT
Bonjour,

I'm having troubles with  AppendChild (same with InsertBefore):

I already have a node:

                        <CLIENT-SERVER-INTERFACE>
                           <SHORT-NAME>MySignal_try</SHORT-NAME>
                           [...]
                        </CLIENT-SERVER-INTERFACE>

And I'm adding to its parent (with AppendChild):

                        <CLIENT-SERVER-INTERFACE>
                           <SHORT-NAME>MySignal</SHORT-NAME>
                           [...]
                        </CLIENT-SERVER-INTERFACE>

In this case, the first node disappears, only the second one remains.
It seems that the first one is recognized and removed because it starts
with the same string... Is there any known limitation on the string length?

Thanks !

G.
RedGrittyBrick - 28 Jan 2008 22:59 GMT
> Bonjour,
Bonsoir,

> I'm having troubles with  AppendChild (same with InsertBefore):

Names starting with capital letters should be class names. Yet the
verb-noun structure suggests method names.

> I already have a node:

A lower case initial letter suggests a variable or method name.

You don't actually say, but I guess you might be thinking about
interface org.w3c.dom.Node and it's methods appendChild() and
insertBefore().

>                         <CLIENT-SERVER-INTERFACE>
>                            <SHORT-NAME>MySignal_try</SHORT-NAME>
>                            [...]
>                         </CLIENT-SERVER-INTERFACE>

That's more than a single node from my perspective.

> And I'm adding to its parent (with AppendChild):
>
[quoted text clipped - 6 lines]
> It seems that the first one is recognized and removed because it starts
> with the same string... Is there any known limitation on the string length?

The limitations are not so short as that, does your XML document have a
DTD that says otherwise?

It's not clear if CLIENT-SERVER-INTERFACE is the "parent" you speak of
or is the node you are adding to something unmentioned.

An SSCCE might make things clearer. Here's one I hastily cobbled together.

--------------------------------------8<------------------------------
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class DomExample {
    public static void main(String[] args) {

        DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            System.exit(1);
        }
        DOMImplementation impl = builder.getDOMImplementation();

        // Create document

        Document doc = impl.createDocument(null, null, null);

        Element e0 = doc.createElement("PARENT");
        doc.appendChild(e0);

        Element e1 = doc.createElement("CLIENT-SERVER-INTERFACE");
        e0.appendChild(e1);

        Element e2 = doc.createElement("SHORTNAME");
        e2.appendChild(doc.createTextNode("mySignal_try"));
        e1.appendChild(e2);

        Element e3 = doc.createElement("CLIENT-SERVER-INTERFACE");
        e0.appendChild(e3);

        Element e4 = doc.createElement("SHORTNAME");
        e4.appendChild(doc.createTextNode("mySignal"));
        e3.appendChild(e4);

        // Print XML

        OutputFormat of = new OutputFormat("XML","ISO-8859-1",true);
        of.setIndent(1);
        of.setIndenting(true);
        of.setDoctype(null, "example.dtd");
        XMLSerializer serializer = new XMLSerializer(System.out ,of);
        try {
            serializer.asDOMSerializer();
            serializer.serialize(doc.getDocumentElement() );
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
--------------------------------------8<------------------------------

Output as follows

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE PARENT SYSTEM "example.dtd">
<PARENT>
    <CLIENT-SERVER-INTERFACE>
        <SHORTNAME>mySignal_try</SHORTNAME>
    </CLIENT-SERVER-INTERFACE>
    <CLIENT-SERVER-INTERFACE>
        <SHORTNAME>mySignal</SHORTNAME>
    </CLIENT-SERVER-INTERFACE>
</PARENT>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.