menu


Subscribe to the blog

Social networks

My page on vk.com
My page on Facebook
My page on Google plus
My page on Twitter

Main - Blog - Java XML - XML record in a java application

Programming Date of creation: Views: 1262

Java XML - XML record in a java application

lesnih.info


Russia

Writing XML in Java

In order to write an XML document in a java application, you need to place the appropriate code that will execute this command, and also make the settings:

1. Create a project in the IDE, for example with the name Java_XML (Automatically created package  java_xml, and in it the main class: Java_XML.java)


And now, the code for the class: 

Java_XML.java

package java_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.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Attr;
 
 
 
public class Java_XML {
 
public static void main(String argv[]) {
 
  try {
 
DocumentBuilderFactory docXML = DocumentBuilderFactory.newInstance();
DocumentBuilder BuilderXML = docXML.newDocumentBuilder();
 
Document XML = BuilderXML.newDocument();
Element rootXML = XML.createElement("company");
XML.appendChild(rootXML);
 
Element product = XML.createElement("product");
rootXML.appendChild(product);
 
                    Attr attr = XML.createAttribute("id");
                    attr.setValue("1");
                    product.setAttributeNode(attr);
 
 
                    Element title = XML.createElement("title");
                    title.appendChild(XML.createTextNode("Mobile iphone"));
                    product.appendChild(title);
 
                    Element color = XML.createElement("color");
                    color.appendChild(XML.createTextNode("black"));
                    product.appendChild(color);
                    
                    Element price = XML.createElement("price");
                    price.appendChild(XML.createTextNode("1000"));
                    product.appendChild(price);
                    
                    Element quantity = XML.createElement("quantity");
                    quantity.appendChild(XML.createTextNode("24"));
                    product.appendChild(quantity);
 
                    
                    
                    
           
Element product2 = XML.createElement("product");
rootXML.appendChild(product2);
 
                    Attr attr2 = XML.createAttribute("id");
                    attr2.setValue("2");
                    product2.setAttributeNode(attr2);
 
                    Element title2 = XML.createElement("title");
                    title2.appendChild(XML.createTextNode("Mobile Samsung"));
                    product2.appendChild(title2);
 
                    Element color2 = XML.createElement("color");
                    color2.appendChild(XML.createTextNode("white"));
                    product2.appendChild(color2);
                    
                    Element price2 = XML.createElement("price");
                    price2.appendChild(XML.createTextNode("995"));
                    product2.appendChild(price2);
                    
                    Element quantity2 = XML.createElement("quantity");
                    quantity2.appendChild(XML.createTextNode("52"));
                    product2.appendChild(quantity2);
 
TransformerFactory transformerFactoryXML = TransformerFactory.newInstance();
Transformer transformerXML = transformerFactoryXML.newTransformer();
DOMSource sourceXML = new DOMSource(XML);
StreamResult resultXML = new StreamResult(new File("C:\\NewFolder1\\text.xml"));
 
transformerXML.transform(sourceXML, resultXML);
 
System.out.println("Ok - file saved!");
 
  } catch (ParserConfigurationException pce) {
pce.printStackTrace();
  } catch (TransformerException tfe) {
tfe.printStackTrace();
  }
}
}


The result of the program - text.xml

As a result of the program, a file will be generated, depending on which path you specify in the code, the file should appear in that place. For example, I had this file written here C:\NewFolder1\text.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<company>
       <product id="1">
              <title>Mobile iphone</title>
              <color>black</color>
              <price>1000</price>
              <quantity>24</quantity>
       </product>
       <product id="2">
              <title>Mobile Samsung</title>
              <color>white</color>
              <price>995</price>
              <quantity>52</quantity>
       </product>
</company>


Java XML - XML record in a java application
Rating: 4.91 | Тotal votes: 11 | Please, rate this article




Commets:

  • qqwerqwer 2018-09-14 17:50:14
    How do connect into mysql? Reply











    Subscribe to the blog


    Random articles from the blog:


    Comparison of methods and functions in Javascript, Java, PHP

    Comparison of methods and functions in Javascript, Java, PHP
    Topic: Programming

    Tags: java,javascript,php

    Views:226

    Mysql Java - INSERT - add data to Mysql

    Mysql Java - INSERT - add data to Mysql
    Topic: Programming

    Tags: java,mysql,insert

    Views:217

    Post java - Send POST request in Java

    Post java - Send POST request in Java
    Topic: Programming

    Tags: java,post,php,integration,exchange

    Views:326