Node value in a DOM model

C

Cyril Vi?ville

I'm using a DOM model to create a XML file of usual connections.
All is good but when i'm using the "getMachine" method, i always have
textnode value to null for TEXT_NODE nodes.

my XML file is something like that for 1 usual connection
<?xml version="1.0" encoding="UTF-8" ?>
- <connexion>
- <machine>
<adresse>Captain</adresse>
<port>6445</port>
<systeme>Linux</systeme>
</machine>
</connexion>

My java code :

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.apache.xml.serialize.*;

public class DOMParsing {
private Document Doc;

public DOMParsing() {
try {
File XMLFile = new File("connexions.xml");
DocumentBuilderFactory Factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder Builder = Factory.newDocumentBuilder();
if (! XMLFile.exists()) {
Doc = Builder.newDocument();
Element RootElem = Doc.createElement("connexion");
Doc.appendChild(RootElem);
}
else {
Doc = Builder.parse(XMLFile);
XMLFile.delete();
fileModify();
}
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}

public void addMachine(String Address, String Port, String Systm) {
Node RootElem = Doc.getFirstChild();
Element Elem = Doc.createElement("machine");
RootElem.appendChild(Elem);
Element FirstElem = Doc.createElement("adresse");
FirstElem.appendChild(Doc.createTextNode(Address));
Elem.appendChild(FirstElem);
Element SecondElem = Doc.createElement("port");
SecondElem.appendChild(Doc.createTextNode(Port));
Elem.appendChild(SecondElem);
Element ThirdElem = Doc.createElement("systeme");
ThirdElem.appendChild(Doc.createTextNode(Systm));
Elem.appendChild(ThirdElem);
fileModify();
}

public void deleteMachine(int placement) {
Element Machine = (Element)
Doc.getElementsByTagName("machine").item(placement);
Machine.getParentNode().removeChild(Machine);
fileModify();
}

public void getMachine(int placement) {
Node Machine = Doc.getElementsByTagName("machine").item(placement);
NodeList Nodes = Machine.getChildNodes();
for (int Number = 0; Number < Nodes.getLength(); Number++) {
Node Child = Nodes.item(Number);
if (Child.getNodeType() != Node.TEXT_NODE) {
System.out.println("Mixed content! Skipping child element " +
Child.getNodeName());
continue;
}
else {
System.out.println("Hello " + Child.getNodeName());
}
}
}

private void fileModify() {
try {
XMLSerializer XMLOut = new XMLSerializer(new
FileWriter("connexions.xml"),
new OutputFormat("xml",
"UTF-8", true));
XMLOut.serialize(Doc);
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
 
S

Steve W. Jackson

:I'm using a DOM model to create a XML file of usual connections.
:All is good but when i'm using the "getMachine" method, i always have
:textnode value to null for TEXT_NODE nodes.
:
:my XML file is something like that for 1 usual connection
:<?xml version="1.0" encoding="UTF-8" ?>
:- <connexion>
: - <machine>
: <adresse>Captain</adresse>
: <port>6445</port>
: <systeme>Linux</systeme>
: </machine>
: </connexion>
:
:My java code :
:
:import java.io.*;
:import org.w3c.dom.*;
:import javax.xml.parsers.*;
:import org.apache.xml.serialize.*;
:
:public class DOMParsing {
: private Document Doc;
:
: public DOMParsing() {
: try {
: File XMLFile = new File("connexions.xml");
: DocumentBuilderFactory Factory =
:DocumentBuilderFactory.newInstance();
: DocumentBuilder Builder = Factory.newDocumentBuilder();
: if (! XMLFile.exists()) {
: Doc = Builder.newDocument();
: Element RootElem = Doc.createElement("connexion");
: Doc.appendChild(RootElem);
: }
: else {
: Doc = Builder.parse(XMLFile);
: XMLFile.delete();
: fileModify();
: }
: }
: catch (Exception ex) {
: System.out.println(ex.toString());
: }
: }
:
: public void addMachine(String Address, String Port, String Systm) {
: Node RootElem = Doc.getFirstChild();
: Element Elem = Doc.createElement("machine");
: RootElem.appendChild(Elem);
: Element FirstElem = Doc.createElement("adresse");
: FirstElem.appendChild(Doc.createTextNode(Address));
: Elem.appendChild(FirstElem);
: Element SecondElem = Doc.createElement("port");
: SecondElem.appendChild(Doc.createTextNode(Port));
: Elem.appendChild(SecondElem);
: Element ThirdElem = Doc.createElement("systeme");
: ThirdElem.appendChild(Doc.createTextNode(Systm));
: Elem.appendChild(ThirdElem);
: fileModify();
: }
:
: public void deleteMachine(int placement) {
: Element Machine = (Element)
:Doc.getElementsByTagName("machine").item(placement);
: Machine.getParentNode().removeChild(Machine);
: fileModify();
: }
:
: public void getMachine(int placement) {
: Node Machine = Doc.getElementsByTagName("machine").item(placement);
: NodeList Nodes = Machine.getChildNodes();
: for (int Number = 0; Number < Nodes.getLength(); Number++) {
: Node Child = Nodes.item(Number);
: if (Child.getNodeType() != Node.TEXT_NODE) {
: System.out.println("Mixed content! Skipping child element " +
:Child.getNodeName());
: continue;
: }
: else {
: System.out.println("Hello " + Child.getNodeName());
: }
: }
: }
:
: private void fileModify() {
: try {
: XMLSerializer XMLOut = new XMLSerializer(new
:FileWriter("connexions.xml"),
: new OutputFormat("xml",
:"UTF-8", true));
: XMLOut.serialize(Doc);
: } catch (Exception ex) {
: System.out.println(ex.toString());
: }
: }
:}

First, a note regarding coding conventions. The preferred practice
among most Java developers is that class and interface names begin with
upper case letters, while all variable and method names begin with lower
case letters. You'll find that many of us who offer assistance will
encourage you to follow this convention in all your Java code.

As to the problem you're encountering, you've got more than one issue in
the above code.

Your addMachine() method begins with a getFirstChild() call on the
Document, but you've got other code that clearly indicates you know
about the getDocumentElement() method you should be using there.

In your getMachine() method, you're executing getElementsByTagName()
against the document rather than its root element, which may well be the
source of your first real trouble. But more to the point, you're
assuming that the Machine variable does in fact contain a non-null Node.
What if the document doesn't contain as many "machine" elements as your
"placement" variable would imply? The result is that you're going to
get a NullPointerException on the next line. So perhaps you should
cover that possibility and respond with some indication that the
placement value isn't valid.

HTH.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top