⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createxml.java

📁 An XML application used to store some data
💻 JAVA
字号:

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class CreateXML 
{
	public CreateXML() 
	{
    }
    public static void main(String[] args) 
    {
        DocumentBuilderFactory factory;
        DocumentBuilder builder;
        Document document =null;
        BufferedReader inr =null;
        
        TransformerFactory transFactory ;
        Transformer transformer ;
        DOMSource source ;
        
        Element root = null;
        Element addr = null;
        Element ch = null;
        Node tx = null;
        String name   = " ";
        String street = " ";
        String city   = " ";
        String pin = " ";
        boolean endloop = false;
        String cont = " ";
        
        try
            {
                factory = DocumentBuilderFactory.newInstance();
                factory.setIgnoringElementContentWhitespace(true);
                System.out.println("igonring whitespace: "+factory.isIgnoringElementContentWhitespace());
                builder = factory.newDocumentBuilder();
                document = builder.newDocument();
                inr = new BufferedReader(new InputStreamReader(System.in));
                root=document.createElement("ADDRBOOK");
               
            }
            catch(Exception e)
            {
                System.out.println("exception in parser creation:\n"+e);
            }
        
            try
            {
                System.out.println("Creating Address Book");
                while(!endloop)
                {
                    
                    addr = document.createElement("ADDR");
                    root.appendChild(addr);   
                    System.out.println("Enter name :");
                    name = inr.readLine();
                    System.out.println("Enter Street :");
                    street = inr.readLine();
                    System.out.println("Enter city :");
                    city = inr.readLine();
                    System.out.println("Enter Pin :");
                    pin = inr.readLine();
                
                
                    ch = document.createElement("NAME");
                    tx = document.createTextNode(name);
                    ch.appendChild(tx);
                    addr.appendChild(ch);
                
                    ch = document.createElement("STREET");
                    tx = document.createTextNode(street);
                    ch.appendChild(tx);
                    addr.appendChild(ch);
                
                    ch = document.createElement("CITY");
                    tx = document.createTextNode(city);
                    ch.appendChild(tx);
                    addr.appendChild(ch);
                
                    ch = document.createElement("PIN");
                    tx = document.createTextNode(pin);
                    ch.appendChild(tx);
                    addr.appendChild(ch);
                
                    root.appendChild(addr);
                
                    System.out.println("Do You Want to enter new record, Press Y - yes N - No");
                    cont = inr.readLine();
                    if(cont.equals("N")|cont.equals("n"))
                    {
                        endloop = true;
                    }
                
                
                }
                
               document.appendChild(root);
                
            }
            catch(Exception e){
                
                System.out.println("Exception in creation of child nodes: "+e);
                
            }
            try
            {
                transFactory =  TransformerFactory.newInstance();
                transformer = transFactory.newTransformer();
                source = new DOMSource(document);
                
                File newXML = new File("myaddr.xml");
                FileOutputStream os = new FileOutputStream(newXML);
                StreamResult result = new StreamResult(os);
                transformer.transform(source, result);

            }
            catch(Exception e)
            {
                
            }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -