Results 1 to 2 of 2

Thread: Help with JDOM

  1. #1
    Join Date
    Nov 2010
    Posts
    1

    question Help with JDOM

    I can create XML output with JDOM with a single pass through the code, however I need to break the code up into suitable methods, so that the XML instance gets progressively built as information is formatted within the programme. Also the final output could be created at a number of points anywhere in the code.
    I have singularly failed to do this.

    Following is my code for a single pass, however in practice there is an iteration taking place that needs to create the elements conn and connName multiple times within the root and a call to another class needs to take place between the creation of the data for conn and connName.
    Any suggestions would be appreciated (this is my first ever Java app, so I understand I may be overlooking something very basic)


    package com.bliss;

    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.XMLOutputter;

    public class XMLWrite {
    public XMLWrite() {
    super();
    }

    public void createBlankXML(String paramType, String inData, String outData) {

    Element root = new Element("AuditLog");
    Element conn = new Element("SendMessage");
    conn.setText(inData) ;
    root.addContent(conn);

    Element connName = new Element("ReturnMessage");
    connName.setText(outData);
    root.addContent(connName);

    Document doc = new Document(root);

    try {
    BufferedOutputStream mainXML =
    new BufferedOutputStream(new FileOutputStream(System.getProperty("user.dir", "") +
    System.getProperty("file.separator") +
    GConst.AUDIT_FOLDER +
    System.getProperty("file.separator") +
    paramType));



    XMLOutputter serializer = new XMLOutputter();
    serializer.output(doc, mainXML);
    Logging.log(GConst.LOG_INFO, XMLWrite.class.getName(), paramType + "created.");
    } catch (IOException e) {
    System.err.println(e);
    }
    }
    }

  2. #2
    Join Date
    Jun 2006
    Posts
    623

    Re: Help with JDOM

    The following class has methods for creating and parsing an xml document using JDom.

    Code:
    package in.techdive.java;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;
    
    public class XMLJDomParser
    {
            public static void main(String[] args)
            {
                    XMLJDomParser xmlJDomParser = new XMLJDomParser();
                    String fileName = "Devices.xml";
                    // first create a xml document and pass the xml string to create an xml file
                    xmlJDomParser.createFile(xmlJDomParser.createDocument(), fileName);
                    // parse the same document
                    xmlJDomParser.parseDoc(fileName);
    
            }
    
            /*
             * This method parses each element in the xml document and prints the text content.
             */
            public void parseDoc(String fileName)
            {
                    Document doc = null;
                    InputStream input = null;
                    SAXBuilder saxBuilder = null;
    
                    try
                    {
                            input = new FileInputStream(fileName);
                            saxBuilder = new SAXBuilder(false);
                            doc = saxBuilder.build(input);
                            Element root = doc.getRootElement();
                            parseElement(root);
                    }
                    catch (Exception e)
                    {
    
                    }
            }
    
            private void parseElement(Element root)
            {
    
                    List<Element> lstElements = root.getChildren();
    
                    for (Element elem : lstElements)
                    {
                            if (elem.getChildren().size() > 0)
                            {
                                    parseElement(elem);
                                    return;
                            }
                            else
                                    System.out.println(elem.getText());
                    }
            }
    
            /*
             * This method creates an xml file using the xmlstirng and file name as arguments.
             */
            public void createFile(String fileContents, String fileName)
            {
                    File file = new File(fileName);
                    FileOutputStream fOut = null;
                    try
                    {
                            fOut = new FileOutputStream(file);
                    }
                    catch (FileNotFoundException e)
                    {
    
                            e.printStackTrace();
                    }
                    BufferedOutputStream bf = new BufferedOutputStream(fOut);
                    try
                    {
                            bf.write(fileContents.getBytes());
                            bf.flush();
    
                    }
                    catch (IOException e)
                    {
                            e.printStackTrace();
                    }
            }
    
            /*
             * This method creates a xml document using jdom.
             */
            public String createDocument()
            {
                    String xmlDoc = null;
    
                    try
                    {
                            Document doc = new Document(new Element("Devices"));
                            Element device = new Element("Device");
    
                            Element deviceId = new Element("deviceId").setText("1001");
                            Element deviceName = new Element("deviceName").setText("Cables Connectors");
                            Element productId = new Element("productId").setText("CC1084");
                            Element serialNo = new Element("serialNo").setText("038123-45627");
    
                            doc.getRootElement().addContent(device);
                            device.addContent(deviceId);
                            device.addContent(deviceName);
                            device.addContent(productId);
                            device.addContent(serialNo);
    
                            XMLOutputter domstream = new XMLOutputter();
                            domstream.output(doc, System.out);
                            xmlDoc = domstream.outputString(doc);
                    }
                    catch (Exception e)
                    {
                            e.printStackTrace();
                    }
                    return xmlDoc;
            }
    }

Similar Threads

  1. How to create an XML document using JDOM?
    By Juany in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 04:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,637,279.80591 seconds with 17 queries