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());
}
}
}
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());
}
}
}