Java XML dosyası oluşturma kodu kullanımı örneği (Java create XML file code usage example)

Java ile XML dosyası oluşturmak için aşağıdaki telefon defteri örneğinden yaralanabilirsiniz.

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

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

public class CreateXmlFile {

    public static void main(String[] args) {
        System.out.println("Starting Creating XML file operation.");

        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

            Document document = documentBuilder.newDocument();
            Element phoneBookRootElement = document.createElement("PhoneBook");
            document.appendChild(phoneBookRootElement);

            Element memberElement = document.createElement("Member");
            phoneBookRootElement.appendChild(memberElement);
            memberElement.setAttribute("id", "01");

            Element firstnameElement = document.createElement("Firstname");
            Text firstnameText = document.createTextNode("Süleyman");
            firstnameElement.appendChild(firstnameText);
            memberElement.appendChild(firstnameElement);

            Element middlenameElement = document.createElement("Middlename");
            Text middlenameText = document.createTextNode("Oğuzhan");
            middlenameElement.appendChild(middlenameText);
            memberElement.appendChild(middlenameElement);

            Element lastnameElement = document.createElement("Lastname");
            lastnameElement.appendChild(document.createTextNode("KINIK"));
            memberElement.appendChild(lastnameElement);

            Element phoneNumberElement = document.createElement("PhoneNumber");
            phoneNumberElement.appendChild(document.createTextNode("0123456789"));
            memberElement.appendChild(phoneNumberElement);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(new File("/media/72C6C53DC6C50279/Documents and Settings/oguzhankinik/sites/primeriesLinux/xmlFileExample01.xml"));

            transformer.transform(domSource, streamResult);

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }

        System.out.println("Successfully finished Creating XML file operation.");
    }
}

Gösterdiğiniz dosya dizininde xmlFileExample01.xml dosyasının varolduğunu varsayarak işlem yapılmıştır. Kodun çalıştırılması sonucunda .xml dosyasının içeriği aşağıdaki gibi olacaktır.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<PhoneBook>
    <Member id="01">
        <Firstname>Süleyman</Firstname>
        <Middlename>Oğuzhan</Middlename>
        <Lastname>KINIK</Lastname>
        <PhoneNumber>0123456789</PhoneNumber>
    </Member>
</PhoneBook>
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on StumbleUpon0Share on Reddit0

Leave a Reply


*