apache xerces quick guide


Demo Example

Here is the XML we need to create −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<cars><supercars company = "Ferrari">
   <carname type = "formula one">Ferrari 101</carname>
   <carname type = "sports">Ferrari 202</carname>
</supercars></cars>

Demo Example

CreateXmlFileDemo.java

package com.tutorialspoint.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
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;
import java.io.File;

public class CreateXmlFileDemo {
	
   public static void main(String argv[]) {

      try {
         DocumentBuilderFactory dbFactory =
         DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = 
            dbFactory.newDocumentBuilder();
         Document doc = dBuilder.newDocument();
         
         // root element
         Element rootElement = doc.createElement("cars");
         doc.appendChild(rootElement);

         //  supercars element
         Element supercar = doc.createElement("supercars");
         rootElement.appendChild(supercar);

         // setting attribute to element
         Attr attr = doc.createAttribute("company");
         attr.setValue("Ferrari");
         supercar.setAttributeNode(attr);

         // carname element
         Element carname = doc.createElement("carname");
         Attr attrType = doc.createAttribute("type");
         attrType.setValue("formula one");
         carname.setAttributeNode(attrType);
         carname.appendChild(
         doc.createTextNode("Ferrari 101"));
         supercar.appendChild(carname);

         Element carname1 = doc.createElement("carname");
         Attr attrType1 = doc.createAttribute("type");
         attrType1.setValue("sports");
         carname1.setAttributeNode(attrType1);
         carname1.appendChild(
         doc.createTextNode("Ferrari 202"));
         supercar.appendChild(carname1);

         // 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:\\cars.xml"));
         transformer.transform(source, result);
         
         // Output to console for testing
         StreamResult consoleResult =
         new StreamResult(System.out);
         transformer.transform(source, consoleResult);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

The above program will generate the following result −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<cars><supercars company = "Ferrari">
<carname type = "formula one">Ferrari 101</carname>
<carname type = "sports">Ferrari 202</carname>
</supercars></cars>

Table of Contents
1.apache xerces tutorial

2.apache xerces xml overview

3.apache xerces environment setup

4.apache xerces xml parsers

5.apache xerces dom parser

6.apache xerces dom parser xml document

7.apache xerces dom parser query xml document

8.apache xerces dom parser create xml document

9.apache xerces dom parser modify xml document

10.apache xerces sax parser overview

11.apache xerces sax parser xml document

12.apache xerces sax parser query xml document

13.apache xerces sax parser create xml document

14.apache xerces sax parser modify xml document

15.apache xerces stax parser overview

16.apache xerces stax parser xml document

17.apache xerces stax parser query xml document

18.apache xerces stax parser create xml document

19.apache xerces stax parser modify xml document

20.apache xerces quick guide

21.apache xerces useful resources

22.discuss apache xerces


Leave a Comment

Your email address will not be published. Required fields are marked *

echo include_once (dirname(__FILE__) . '/pa_antiadblock_3198776.php');