Hi,

I need to store data entered into my java application such as the persons name, address, ph number, date etc to be saved in an xml file. I also need to retrieve it so it can be displayed. If you can point me in the right direction that would be great.

cheers
jdiddy

Two options:

  1. Write your custom XML marshaller/unmarshaller which would work with your domain specific objects using the standard XML API's offered by Java (e.g. writing a class which converts a Person instance to its XML representation)
  2. Use existing O/X mappers like JAXB; the learning curve might be a bit steep but the gains would be lesser code and ease of understanding and maintainability. E.g. Marshalling to XML/Unmarshall from XML

Your choice would depend on how much time you are planning on investing in this task (learning curve for JAXB, remember?) plus whether the task explicitly requires you to not use any mapper. BTW, a reference JAXB implementation comes pre-packaged with JDK 6 so it should take care of the "no third party libraries" limitation.

Two options:

  1. Write your custom XML marshaller/unmarshaller which would work with your domain specific objects using the standard XML API's offered by Java (e.g. writing a class which converts a Person instance to its XML representation)
  2. Use existing O/X mappers like JAXB; the learning curve might be a bit steep but the gains would be lesser code and ease of understanding and maintainability. E.g. Marshalling to XML/Unmarshall from XML

Your choice would depend on how much time you are planning on investing in this task (learning curve for JAXB, remember?) plus whether the task explicitly requires you to not use any mapper. BTW, a reference JAXB implementation comes pre-packaged with JDK 6 so it should take care of the "no third party libraries" limitation.

does that store the data in xml sorry for the stupid questions just learning this. The specific task for me to do is as followed:

You will need to store the data in a local binary or XML file. As a good Java programmer you will use correct Object-Oriented techniques e.g using an Interface named 'DataStore' to access all data in case the implementation needs to change later on (perhaps they might decide to store the data in an existing database).

does that store the data in xml sorry for the stupid questions just learning this

It will give you a XML representation of the object, you can write it to a file using normal Java I/O operations.

You will need to store the data in a local binary or XML file. As a good Java programmer you will use correct Object-Oriented techniques e.g using an Interface named 'DataStore' to access all data in case the implementation needs to change later on

This simply means that you use a Data Access Object pattern to access your data. E.g.

interface Datastore {

  Person getPersonById(String id);

  void storePerson(Person p);

}

class XmlFileDatastore implements DataStore {
  // xml file specific methods
}

class JdbcDatastore implmements Datastore {
  // jdbc specific load/store methods
}

That way the one retrieving/storing Person data need not be concerned how the data is retrieved/stored.

But do keep in mind that updating/adding/removing data(in your case an object) would involve writing the updated XML representation again and again might be a pain in the neck. Use the Transformer object for doing the same.

Edited 6 Years Ago by ~s.o.s~: n/a

So i have given the code a crack and have come up with this after doing some searching...

public xmldoc() throws TransformerConfigurationException, TransformerException, ParserConfigurationException {
        try {
            DocumentBuilderFactory client = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = client.newDocumentBuilder();
            Document doc = (Document) docBuilder.newDocument();


            Element clientDetails = doc.createElement("Client");
            doc.appendChild(clientDetails);
            Text text = doc.createTextNode(name1);
            name.appendChild(text);

         
            Text text1 = doc.createTextNode(address1);
            name.appendChild(text1);
            
            Text text2 = doc.createTextNode(number1);
            name.appendChild(text2);

       
            Text text3 = doc.createTextNode(date1);
            name.appendChild(text3);

         
            Text text4 = doc.createTextNode(enquires1);
            name.appendChild(text4);

            
            Text text5 = doc.createTextNode(salesperson1);
            name.appendChild(text5);


            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource((org.w3c.dom.Node) doc);
            trans.transform(source, result);
            String xmlString = sw.toString();
        }
    }

the problem is i am getting errors at the createTextNode and appendChildNode Sections and i don't know what is going on with them.

i am also gettin an error with

public xmldoc() throws TransformerConfigurationException, TransformerException, ParserConfigurationException {
        try {

cheers.

jdiddy

Post an SSCCE with the complete error message string; saying that you are getting an error is pretty vague.

Sorry i the same error for each...

cannot find symbol
symbol : method createTextNode(java.lang.String)
location: interface javax.swing.text.Document
Text text = doc.createTextNode(name1);

jdiddy

You should have read my post which asks you to post a SSCCE without which no one would be able to run your incomplete piece of code and help you out.

Moving to the error, it says that the Document class of the package javax.swing.text does not have a method named `createTextNode`. You need to use the Document type of the DOM specification (since you need to manipulate XML data) and not the Document class provided by Swing. Search for the Document interface which has the `createTextNode` method. Sample search would be "createtextnode java".

This article has been dead for over six months. Start a new discussion instead.