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>