JAVA XML

R4R® Java Java XML JAVA XML Basics

AltStyle によって変換されたページ (->オリジナル) /

(追記) (追記ここまで)
(追記) (追記ここまで)
Create XML Document

With help of this program, we will be learn to create a XML document using the DOM Application programming interfaces. In This XML document uses 1.0 version and UTF-8 encoding. By help of this program we can create a XML document on the console. This program asks for the number of elements to be added in the generated xml file. It takes the root name at the console and passes it in the createElement() method. It creates the Element object and invokes the Document object . Depending upon the given number, it creates that much elements and fills them with data. Finally, it displays the generated XML file with its version and encoding.

Example

Here is Java File: Studentrecord.java

(追記) (追記ここまで)
package r4r;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
[an error occurred while processing this directive]
public class createxml {
	public static void main(String[] args) throws Exception {
		 BufferedReader br = new BufferedReader(
		 new InputStreamReader(System.in));
		System.out.print("Enter number to add elements in our XML file: ");
		 String str = br.readLine();
		 int nu = Integer.parseInt(str);
		 System.out.print("Enter root: ");
		 String root = br.readLine();
		 DocumentBuilderFactory docFact = DocumentBuilderFactory.newInstance();
		 DocumentBuilder docBuild = docFact.newDocumentBuilder();
		 Document docu = docBuild.newDocument();
		 Element rEle = docu.createElement(root);
		 docu.appendChild(rEle);
		 for (int i = 1; i <= nu; i++){
		 System.out.print("Enter the element: ");
		 String element = br.readLine();
		 System.out.print("Enter the data: ");
		 String data = br.readLine();
		 Element em = docu.createElement(element);
		 em.appendChild(docu.createTextNode(data));
		 rEle.appendChild(em);
		 }
		 TransformerFactory transFact = TransformerFactory.newInstance();
		 Transformer trans = transFact.newTransformer();
		 DOMSource dsrc = new DOMSource(docu);
		 StreamResult result = new StreamResult(System.out);
		 trans.transform(dsrc, result);
		 }
}
(追記) (追記ここまで)
Copyright ©2021-22 r4r.co.in, all rights reserved. Theguestspost.com
Sitemap
Career
Post comment
About us
Subscription
Unsubscription