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

}

}

}

[3230 byte] By [paul_carrona] at [2007-11-27 11:25:35]
# 1

How is this other block of text any different from any other element in this XML document?

georgemca at 2007-7-29 16:05:18 > top of Java-index,Java Essentials,New To Java...
# 2

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

PatrickFinnigana at 2007-7-29 16:05:18 > top of Java-index,Java Essentials,New To Java...
# 3

> 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

georgemca at 2007-7-29 16:05:18 > top of Java-index,Java Essentials,New To Java...
# 4

sorry, i misread the original question. please disregard my answer

PatrickFinnigana at 2007-7-29 16:05:18 > top of Java-index,Java Essentials,New To Java...
# 5

>

> 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

true and jdom is not an efficient way to create xml anyways, specially if down the road the size of the xml grows. Try XMLBeans or JAXB or Castor etc.

kilyasa at 2007-7-29 16:05:18 > top of Java-index,Java Essentials,New To Java...
# 6

> sorry, i misread the original question. please

> disregard my answer

Even so, under what circumstances would you circumvent the XML library you're using, and output "<tag>content</tag>" yourself?

georgemca at 2007-7-29 16:05:19 > top of Java-index,Java Essentials,New To Java...
# 7

to output something that is not in memory as an XML library object?

PatrickFinnigana at 2007-7-29 16:05:19 > top of Java-index,Java Essentials,New To Java...
# 8

> true and jdom is not an efficient way to create xml

> anyways, specially if down the road the size of the

> xml grows. Try XMLBeans or JAXB or Castor etc.

I'm new to DOM in Java. Why is jdom inefficient?

PatrickFinnigana at 2007-7-29 16:05:19 > top of Java-index,Java Essentials,New To Java...
# 9

> to output something that is not in memory as an XML

> library object?

Don't bother doing that, it's incredibly pointless. You can add new content to the existing content as you need to

georgemca at 2007-7-29 16:05:19 > top of Java-index,Java Essentials,New To Java...