Results 1 to 2 of 2

Thread: XML to Java

  1. #1
    joe99 is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Unhappy XML to Java

    Hi,

    I have the next XML document, and I have to implement it with Java. Can somebody help me?

    <type date="2010-01-01">
    <type2>
    <art nom="A" index="12">
    <art nom="P" index="2">
    </type2>
    <lite>
    <lite2 atrib1="A" atrib2="B">
    </lite>
    </type>

    Now I have:

    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    result = docBuilder.newDocument();

    Element root = result.createElement("type");
    root.setAttribute("date", this.aDate);
    result.appendChild(root);

    for (int i=0; i<this.aPlayers.size(); i++){
    for (int j=0; j<this.aGames.size(); j++){
    Element child = result.createElement("type2");
    }
    }

    but I don't know to continue...

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I would recommend you use StAX. API. It is a more modern than the DOM API, and I think somewhat less verbose to work with.

    For example, the following code creates this exact xml example you provided.

    Java Code:
    import java.io.ByteArrayOutputStream;
    import java.io.OutputStream;
    
    import javax.xml.stream.XMLOutputFactory;
    import javax.xml.stream.XMLStreamException;
    import javax.xml.stream.XMLStreamWriter;
    
    
    /**
     *Created on May 29, 2010
     */
    
    /**
     * @author thein
     *
     */
    public class StaxExample {
    
      /**
       * In this example we just write the XML to a string, but generally we could write to an output stream, or a file too.
       * @return
       */
      public String getXML() {
        
        // write out XML using the StAX API.
        OutputStream out = new ByteArrayOutputStream(); // or what ever the output stream we would want to write to
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = null;
        try {
          writer = factory.createXMLStreamWriter(out);
          
          //if we need to write the xml preambl stuff
          //writer.writeStartDocument("1.0");
          
          // here is where we do the writing of our elements.
          // you would wrap these with your beans and variables, 
          
    //    <type date="2010-01-01">
          writer.writeStartElement("type");
          writer.writeAttribute("date", "2010-01-01");
    
    //    <type2>
          writer.writeStartElement("type2");
          
    //    <art nom="A" index="12">
          writer.writeEmptyElement("art");
          writer.writeAttribute("nom", "A");
          writer.writeAttribute("index", "12");
          
    //    <art nom="P" index="2">
          writer.writeEmptyElement("art");
          writer.writeAttribute("nom", "P");
          writer.writeAttribute("index", "2");
          
    //    </type2>
          writer.writeEndElement();
          
    //    <lite>
          writer.writeStartElement("lite");
          
    //    <lite2 atrib1="A" atrib2="B">
          writer.writeEmptyElement("lite2");
          writer.writeAttribute("atrib1", "A");
          writer.writeAttribute("attrib2", "B");
          
    //    </lite>
          writer.writeEndElement();
        
    //    </type>      
          writer.writeEndElement();
          
          // if we did write the start document, should write end document.
          //writer.writeEndDocument();
          writer.flush();
        } catch (XMLStreamException ex) {
          System.err.println(ex.getMessage());
          ex.printStackTrace();
        } finally {
          if (writer != null) {
            try {
              writer.close();
            } catch (XMLStreamException ex) {
              // ignore
            }
          }
        }
        return out.toString();
      }
      
      /**
       * Runs this test.
       * @param args none.
       */
      public static void main(String[] args) {
        System.out.println(new StaxExample().getXML());
      }
    }
    If you are using Java 1.6, StAX is now built into the JRE. and for java 1.4, 1.5 there a stax.jar file available from the stax home page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •