Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml file generator in java

Tags:

java

xml

I want to make a java swing application from where i can generated xml file which hold same data,and generated number of file will be decided by user.My xml file holds xml schema and my xml file struture is looking like this

<transaction>
  <xs:schema id="transaction" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="id">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name" type="xs:string" minOccurs="0" />
                <xs:element name="sn" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="data">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
                <xs:element name="key" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="productData">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
                <xs:element name="key" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <id>
    <name>smith</tli>
    <sn>1234567</sn>
  </id>
  <data>
    <dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
    <key>book</key>
  </data>
  <productData>
    <dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
    <key>game</key>
  </productData>
</transaction>

I am new in java if some one give some code snipes help, it will be more helpful for me.

I want to generate xml files which hold the xml schema which is given in my example xml.

like image 735
saba Avatar asked Jan 14 '12 20:01

saba


People also ask

How is XML generated in Java?

XML Class Generator for Java creates Java source files from an XML DTD or XML Schema Definition. This is useful in the following situations: When an application wants to send an XML message to another application based on agreed-upon DTDs or XML Schemas. As the back end of a web form to construct an XML document.


1 Answers

In java you use the JAXP to work with XML. You use DOM related classes to generate the xml. Below is a small example of using JAXP to create XML.

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteXMLFile {

    public static void main(String argv[]) {

      try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("company");
        doc.appendChild(rootElement);

        // staff elements
        Element staff = doc.createElement("Staff");
        rootElement.appendChild(staff);

        // set attribute to staff element
        Attr attr = doc.createAttribute("id");
        attr.setValue("1");
        staff.setAttributeNode(attr);

        // shorten way
        // staff.setAttribute("id", "1");

        // firstname elements
        Element firstname = doc.createElement("firstname");
        firstname.appendChild(doc.createTextNode("yong"));
        staff.appendChild(firstname);

        // lastname elements
        Element lastname = doc.createElement("lastname");
        lastname.appendChild(doc.createTextNode("mook kim"));
        staff.appendChild(lastname);

        // nickname elements
        Element nickname = doc.createElement("nickname");
        nickname.appendChild(doc.createTextNode("mkyong"));
        staff.appendChild(nickname);

        // salary elements
        Element salary = doc.createElement("salary");
        salary.appendChild(doc.createTextNode("100000"));
        staff.appendChild(salary);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("C:\\file.xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");

      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (TransformerException tfe) {
        tfe.printStackTrace();
      }
    }
}
like image 167
Aravind Yarram Avatar answered Nov 15 '22 16:11

Aravind Yarram