?
Solved

DOM4J and text placement in XML

Posted on 2004-11-30
3
Medium Priority
?
378 Views
Last Modified: 2008-03-17
Using DOM4J in Java, if i want this:
<george>
   <cool>tru</cool>
</george>

I can go:
Element george = new Element("george")
Element git = new Element("cool")
cool.setText("true");
george.add(cool);

BUT, what do i do if I want to do this:
<george>
    true<cool/>very
 </george>

How can this be achieved?
0
Comment
Question by:blackfrancis75
  • 2
3 Comments
 
LVL 6

Expert Comment

by:sathishv
ID: 12713509
User addText() method instead of setText():

Element george = new Element("george");
george.addText("true");
george.addElement("cool");
george.addText("very");

Hope this helps.

Cheers,
Sathish
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 12722460
I tried:

Element newLine = new Element("new-line");
field.addText("line1")
field.add(newLine);
field.addText("line2");

and I got:

<field>
   line1line2<new-line/>
</field>

what I needed was:

<field>
   line1<new-line/>line2
</field>

0
 
LVL 6

Accepted Solution

by:
sathishv earned 2000 total points
ID: 12724785
Following is an dom4j sample program which I modified to test this case. I got the output as :

<?xml version="1.0" encoding="UTF-8"?>
<root><child1>line1<child2/>line2</child1></root>

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;


public class XmlDom4J{


public void generateDocument(){
Document document = DocumentHelper.createDocument();
     Element rootElement = document.addElement("root");
       Element child1 = rootElement.addElement("child1");
       child1.addText("line1");
       Element child2 = child1.addElement("child2");
       child1.addText("line2");
    try{
    XMLWriter output = new XMLWriter(
            new FileWriter( new File("test.xml") ));
        output.write( document );
        output.close();
        }
     catch(IOException e){System.out.println(e.getMessage());}
}



public static void main(String[] argv){
XmlDom4J dom4j=new XmlDom4J();
dom4j.generateDocument();
}

}
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question