Outputting Text To XML
I am using the following code to create an XML from a text file. Aside from the tags containing the phonenumbers I want to have tags which contain A block of text. How can I output this to my xml file? Would it be something along the lines of a System.out.println("<other_text>bla bla bla</other_text>) or how would I get it to appear in my XML?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
publicclass XML_builder{
public XML_builder(){}
publicstaticvoid main(String[] args){
testBuildXML();
}
privatestaticvoid testBuildXML(){
try{
String XMLFile ="P://Useful Docs/Programming/Java/Automated Texting/Automated Texting/phoneNumbers.xml";
File text = FindLatestFile.getLatest(new File(System.getProperty("user.dir")));
FileReader reader =new FileReader(text);
BufferedReader buffer =new BufferedReader(reader);
String temp ="";
int x = 0;
Element element =new Element("phone_numbers");
while ((temp = buffer.readLine())!=null){
Element child_node =new Element("number_"+x);
System.out.println(temp);
child_node.addContent(temp);
x++;
element.addContent(child_node);
}
FileWriter writer =new java.io.FileWriter(XMLFile);
XMLOutputter xmloutputter =new XMLOutputter();
Document doc =new Document(element);
xmloutputter.output(doc, writer);
}
catch (IOException exception){
System.out.println(exception);
}
}
}
Here's a function I used to print XML elements to a PrintStream:
private void writeNode (PrintStream myPS, Node myNode) {
// TODO: DEBUG
// TODO: worry about attributes!
//StaticMethods.output("Writing node: " + myNode.getNodeName());
if (myNode.getNodeType() == Node.ELEMENT_NODE) {
//StaticMethods.output("Element node encountered");
String openingTag = "<" + myNode.getNodeName().toLowerCase() + ">";
myPS.print(openingTag);
NodeList myChildren = myNode.getChildNodes();
//StaticMethods.output("Writing " + myChildren.getLength() + " child nodes...");
for (int i = 0; i < myChildren.getLength(); i++) {
writeNode (myPS, myChildren.item(i));
}
String closingTag = "</" + myNode.getNodeName().toLowerCase() + ">";
myPS.print(closingTag);
}
else if (myNode.getNodeType() == Node.TEXT_NODE) {
//StaticMethods.output("Text node encountered.");
myPS.print(myNode.getNodeValue());
}
else {
StaticMethods.output("Unrecognized node type " + Short.toString(myNode.getNodeType()) + " encountered.");
}
}
And yeah, if you want to write text, you can just print "<tag>Here is my text.</tag>" to the output file. Keep in mind, you may want to print element nodes on separate lines and use whitespace for indentation so that your XML file is printed out cleanly.
Forgot to mention, this function calls itself recursively and is thus able to print out an entire XML node tree.
Message was edited by:
PatrickFinnigan
> And yeah, if you want to write text, you can just
> print "<tag>Here is my text.</tag>" to the output
> file. Keep in mind, you may want to print element
> nodes on separate lines and use whitespace for
> indentation so that your XML file is printed out
> cleanly.
Why on earth would you go to all the bother of creating an XML document, and some elements, just to hand-whittle parts of it? This makes no sense at all