Re: Problem with AppendChild



Groufo wrote:
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):

<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?

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>

.



Relevant Pages

  • Re: Using XPath Against A Node
    ... top parent? ... Or is the entire xml document sent with it. ... In terms of the XPath implementation with SelectSingleNode and SelectNodes if the node is not inserted in the owning document then it looks like the XPath / evaluates to the node itself. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Please Help: SQLXML Bulk Load Error: Schema: relationship expected on Person
    ... <this is used when your xml document contains data for multiple tables in a ... Parent, child relationship> ... The following is the annotated schema: ... > No parent, ...
    (microsoft.public.sqlserver.xml)
  • Problem Joining Parent and Children
    ... I'm having problems joining the parent and children together from an ... parent rows. ... The XML Document is a little different than what I like to work with, ... exec sp_xml_preparedocument @hdoc output, @doc ...
    (microsoft.public.sqlserver.xml)
  • Re: Using XPath Against A Node
    ... Or is the entire xml document sent with ... of that node was the new parent." ... Using "/" will search the document root node immediate descendants. ... run xpath statements against the xml file to grab data from it, so I use, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: euro sign become ? on xml document parsing
    ... > I've got an XML document that contains euro signs and looks like: ... > DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance; ... > DocumentBuilder builder = factory.newDocumentBuilder; ... > The problem is the euro signs are transformed into the charactere '?' ...
    (comp.lang.java.help)