Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create XML file using java

Tags:

java

xml

How to create a xml file and save it in some place in my machine using java..there are attributes also include in the xml file? I have found org.w3c.dom.Document but having problems with creating attributes for elements and save the xml file.

Thank You.

like image 374
Harshana Avatar asked Nov 10 '10 07:11

Harshana


People also ask

Can we write Java code in XML file?

Read and Write XML with JAXB. JAXB stands for Java Architecture for XML Binding which provides a convenient way for manipulating XML in Java. It is Java standard that defines an API for reading and writing Java objects to and from XML documents. Starting from Java 6, JAXB is a part of the Java Development Kit (JDK).

Can we create an XML Document using DOM parser?

The DOM provides many handy classes to create an XML file easily. Firstly, you have to create a Document with DocumentBuilder class, define all the XML content – node, attribute with Element class. In last, use the Transformer class to output the entire XML content to stream output, typically a File.

What is XML used for in Java?

XML provides a universal syntax for Java semantics (behavior). Simply put, this means that a developer can create descriptions for different types of data to make the data behave in various ways with Java programming code, and can later repeatedly use and modify those descriptions.


2 Answers

You can use a DOM XML parser to create an XML file using Java. A good example can be found on this site:

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:\\testing.xml"));     transformer.transform(source, result);      System.out.println("Done");  }catch(ParserConfigurationException pce){     pce.printStackTrace(); }catch(TransformerException tfe){     tfe.printStackTrace(); } 
like image 128
monica bubna Avatar answered Sep 27 '22 22:09

monica bubna


You can use Xembly, a small open source library that makes this XML creating process much more intuitive:

String xml = new Xembler(   new Directives()     .add("root")     .add("order")     .attr("id", "553")     .set("$140.00") ).xml(); 

Xembly is a wrapper around native Java DOM, and is a very lightweight library.

like image 33
yegor256 Avatar answered Sep 27 '22 22:09

yegor256