Java W3C 생 성 XML

3958 단어 J2SEJ2EEGoogleAndroid
import java.io.File;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.Document;
import org.w3c.dom.Element;

public class CreateRuleXML {

    public static void main(String[] args) {
        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            document.setXmlVersion("1.0");

            Element root = document.createElement("MobileNet");       //        
            document.appendChild(root);                               //       Document    
            
            Element pageElement = document.createElement("page");     //     page   
            pageElement.setAttribute("name", "list.jsp");             //  page   name  

            Element methodElement = document.createElement("method");   //  method  
            methodElement.setTextContent("get");                        // method   
            pageElement.appendChild(methodElement);                     //  method   page   

            Element displayElement = document.createElement("display");    //  method  
            displayElement.setTextContent("list");                         // display   
            pageElement.appendChild(displayElement);            //  display   page    
            
            Element request_paramElement = document.createElement("request_param");
            request_paramElement.setTextContent("request_param1|request_param2");
            pageElement.appendChild(request_paramElement);
            
            root.appendChild(pageElement);
            pageElement = document.createElement("page");                    //     page   
            pageElement.setAttribute("name", "content.jsp");                 //  page   name  

            methodElement = document.createElement("method");
            methodElement.setTextContent("post");
            
            pageElement.appendChild(methodElement);
            displayElement = document.createElement("display");
            displayElement.setTextContent("content");
            
            pageElement.appendChild(displayElement);
            
            Element url_titleElement = document.createElement("url_title");        //  url_title  
            url_titleElement.setTextContent("title,publisher,published_calendar"); // url_title   
            pageElement.appendChild(url_titleElement);                             //  url_title   page   

            root.appendChild(pageElement);                                         // page       

            TransformerFactory transFactory = TransformerFactory.newInstance();     //   Document     
            Transformer transFormer = transFactory.newTransformer();

            DOMSource domSource = new DOMSource(document);                           //      

            File file = new File("MobileNetRule.xml"); //  xml  

            if (!file.exists()) {
                file.createNewFile();
            }

            FileOutputStream out = new FileOutputStream(file);          //                 
            StreamResult xmlResult = new StreamResult(out);            //        
            
            transFormer.transform(domSource, xmlResult);              //  xml              
            System.out.println(file.getAbsolutePath());               //         
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}

좋은 웹페이지 즐겨찾기