xml jdom and database

hello:

I am in need of the following functionality.

I would like to create a JDOM xml document and store it in mysql as a "TEXT".
Alternatively I would like to pull the "TEXT" from MySQL and parse it into a JDOM DOcument.
I tried to google but could not find a satisfactory answer.
if you could let me know the JDOM API calls to articulate the above scenario I would appreciate it very much
Thanks
arjoshi77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaitu chaituCommented:
using below link you will know on how to store xml data .

http://www.roseindia.net/servlets/login-Xml-servlet.shtml

using below link you will know on how to  write an XML document, using JDOM

http://www.java2s.com/Code/Java/XML/MakeupandwriteanXMLdocumentusingJDOM.htm
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

chaitu chaituCommented:
0
for_yanCommented:



I tried this simple example on how to manipulate with XML using JDom, and it works fine:
http://javaneze.wordpress.com/2006/06/09/a-simple-example-using-jdom/


/*
 * PlayWithJDom.java
 * @author javaneze@gmail.com
 */

//package jforjava;

import java.io.File;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.DOMOutputter;
import org.jdom.output.XMLOutputter;

public class PlayWithJDom {

XMLOutputter printer = null;

public PlayWithJDom() {
        //lets create an XML Printer!
        printer = new XMLOutputter();
    }

/**
     * Parsing an XML file and then
     * playing with some basic JDOM functionality!
     * @param anXmlDocFile
     */
    public void parseAndPlay(File anXmlDocFile){
        //create a parser
        SAXBuilder parser = new SAXBuilder();
        try {
            //get the dom-document
            Document doc = parser.build(anXmlDocFile);

// illustrate some jdom tree walk and print sample
            System.out.println(doc.getRootElement().getName());
            Element firstChild = (Element)doc.getRootElement().getChildren().get(0);
            System.out.println(">> "+firstChild.getName()+":"+ firstChild.getAttribute("type").getValue());
            System.out.println(">>>>" +firstChild.getText());

//easily print
            this.printJDom(doc);

// do you want an classic DOM? no prob!
            DOMOutputter out = new DOMOutputter();
            org.w3c.dom.Document domDoc= out.output(doc);
            System.out.println(domDoc.getImplementation().toString());

//lets print in the console the JDomTree
            this.printJDom(doc);

//lets make some changes!
            Element child =(Element) doc.getRootElement().getChildren().get(0);
            child.setName("NewNAME");
            child.setText("Water Mellon");

//lets print in the console the altered JDomTree
            this.printJDom(doc);

} catch (JDOMException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

/**
     * Using the XMLOuputer prints in the Console
     * the JDOM tree!
     * @param aJDOMdoc A JDOM Document
     * @throws java.io.IOException
     */
    public void printJDom(Document aJDOMdoc) throws IOException{
        System.out.println("<------------XML DOCUMENT------------>");
        this.printer.output(aJDOMdoc, System.out);
    }

/**
     * Main Method - Program Start!
     * @param args
     */
    public static void main(String[] args){
        PlayWithJDom play = new PlayWithJDom();
        play.parseAndPlay(new File(args[0]));
    }
}

Open in new window


Input file:
<?xml version="1.0"?>
<food>
<name type="fruit">Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100"/>
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>

Open in new window



Output:
food
>> name:fruit
>>>>Avocado Dip
<------------XML DOCUMENT------------>
<?xml version="1.0" encoding="UTF-8"?>
<food>
<name type="fruit">Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100" />
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl@a39137
<------------XML DOCUMENT------------>
<?xml version="1.0" encoding="UTF-8"?>
<food>
<name type="fruit">Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100" />
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
<------------XML DOCUMENT------------>
<?xml version="1.0" encoding="UTF-8"?>
<food>
<NewNAME type="fruit">Water Mellon</NewNAME>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100" />
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>

Open in new window


I don't think there should be a problem of storing and retrieveing
XML document in MySQL
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
By the way this is the veriosn of JDOM which I used:

http://www.jdom.org/dist/binary/jdom-1.1.2.zip

and jar file jdom-1.1.2.jar form build folder
after you unzip
0
for_yanCommented:

And this is exmaple inserting CLOB data into MySQL database:

http://www.roseindia.net/tutorial/java/jdbc/insertclob.html
0
CEHJCommented:
Try the following
XMLOutputter out = new XMLOutputter();
StringWriter sw = new StringWriter();
out.output(document, sw);
PreparedStatement ps = connection.prepareStatement("insert into mytable(xml_column) values (?)");
ps.setString(1, sw.toString());
ps.executeUpdate();
// close db stuff

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.