转载几篇关于java读写及验证xml的文章(一、How to create XML file in Java )

本文介绍了一种使用Java创建XML文件的方法。通过用户输入元素数量、根节点名称及各元素的具体内容,程序能够生成相应的XML文档并输出到控制台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to create XML file in Java

来自: http://forums.techarena.in/software-development/1169158.htm
Hello Friends,

I am New to the Programming world, and i have started working on JAVA, i have 2 to 3 months experience on it, and am looking forward to create an XML file in Java, so please let me know, how to create XML file in Java.

Thanks in Advance...
Reply With Quote
  # 2  
Old 27-04-2009
Khushal's Avatar
Member
 
Join Date: Mar 2008
Posts: 246
Re: How to create XML file in Java

This program helps in creating 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.

Here is Java File: CreatXMLFile.java

Code:
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
 
public class CreatXMLFile {
  public static void main(String[] args) throws Exception {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter number to add elements in your XML file: ");
    String str = bf.readLine();
    int no = Integer.parseInt(str);
    System.out.print("Enter root: ");
    String root = bf.readLine();
    DocumentBuilderFactory documentBuilderFactory = 
                                   DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = 
                              documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
    Element rootElement = document.createElement(root);
        document.appendChild(rootElement);
    for (int i = 1; i <= no; i++){
      System.out.print("Enter the element: ");
      String element = bf.readLine();
      System.out.print("Enter the data: ");
      String data = bf.readLine();
      Element em = document.createElement(element);
      em.appendChild(document.createTextNode(data));
      rootElement.appendChild(em);
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(System.out);
        transformer.transform(source, result);
  }
}
Reply With Quote
  # 3  
Old 27-04-2009
Suzane's Avatar
Member
 
Join Date: Apr 2009
Posts: 32
Re: How to create XML file in Java

Thanks Khushal this really work out, this is what i wanted to have to create the XML file.

If anyone has different programming logic please let me know.

Thanks once again.
Reply With Quote
  # 4  
Old 27-04-2009
Member
 
Join Date: Oct 2008
Posts: 158
Re: How to create XML file in Java

Here i will also provide you one more java programming example just have a look at it.

Code:
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

/**
 * A sample application which shows how to perform a
 * XML document validation.
 */

public class Test {
  public static void main(String[] args) {
    try {
      // define the type of schema - we use W3C:
      String schemaLang = "http://www.w3.org/2001/XMLSchema";

      // get validation driver:
      SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

      // create schema by reading it from an XSD file:
      Schema schema = factory.newSchema(new StreamSource("sample.xsd"));
      Validator validator = schema.newValidator();

      // at last perform validation:
      validator.validate(new StreamSource("sample.xml"));

    }catch (SAXException ex) {
      // we are here if the document is not valid:
      // ... process validation error...
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值