`
asiainfoywl
  • 浏览: 73559 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

java create xml

    博客分类:
  • JAVA
阅读更多
import java.io.FileOutputStream;   
import java.io.IOException;   
import org.jdom.Document;   
import org.jdom.Element;   
import org.jdom.JDOMException;   
import org.jdom.output.XMLOutputter;   
    
public class Java2XML {   
    
    public void BuildXMLDoc() throws IOException, JDOMException {   
    
       // 创建根节点 list;   
        Element root = new Element("list");   
          
       // 根节点添加到文档中;   
        Document Doc = new Document(root);   
    
       // 此处 for 循环可替换成 遍历 数据库表的结果集操作;   
       for (int i = 0; i < 5; i++) {   
              
           // 创建节点 user;   
           Element elements = new Element("user");   
              
           // 给 user 节点添加属性 id;   
           elements.setAttribute("id", "" + i);   
              
           // 给 user 节点添加子节点并赋值;   
           // new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui 替换成表中记录值;   
           elements.addContent(new Element("name").setText("xuehui"));  
           elements.addContent(new Element("age").setText("28"));  
           elements.addContent(new Element("sex").setText("Male"));  
   
           // 给父节点list添加user子节点;  
           root.addContent(elements);  
   
       }  
        XMLOutputter XMLOut = new XMLOutputter();  
         
       // 输出 user.xml 文件;  
        XMLOut.output(Doc, new FileOutputStream("user.xml"));  
    }  
   
    public static void main(String[] args) {  
       try {  
           Java2XML j2x = new Java2XML();  
           System.out.println("生成 mxl 文件...");  
           j2x.BuildXMLDoc();  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
    }  
   
}  
分享到:
评论

相关推荐

    Java and XML(英文第三版)

    Java and XML, 3rd Edition, shows you how ... You'll learn how to create, read, and modify RSS feeds for syndicated content and use XML to power the next generation of websites with Ajax and Adobe Flash.

    Java XML and JSON: Document Processing for Java SE, 2nd Edition

    Create, validate, parse, and transform XML documents Apply Java’s SAX, DOM, StAX, XPath, and XSLT APIs Master the JSON format for serializing and transmitting data Code against third-party APIs such ...

    java word装pdf,绝对无水印,不要license.xml,不要编译,直接就可以用,你找破天还是这个最好

    只需要导入aspose-words-14.9.0-jdk16.jar 这个jar包,不需要什么license.xml,直接去水印,不需要什么反编译啥的,直接就用了,不好用找我,绝对不骗人,网上好多都是去不了水印,而且还有些根本跑不了,还有就是...

    xml-java-unedifact:一个简单的示例,显示如何使用烟雾转换xml-java-unedifact

    将create_bindingconfigs.sh Shell脚本(-&gt; mescedia / xml-java-unedifact / src / main / resources / create_bindingconfigs.sh)放在smooks / unedifact源文件夹的根目录中并运行它。 这将在子文件夹...

    解决Eclipse启动出错:Failed to create the Java Virtual Machine

    eclipse启动时弹出Failed to create the Java Virtual Machine 一、现象 今天装eclipse的时候出现Failed to create the Java Virtual Machine 的错误。 错误图片如下:  二、出错原因 把错误提示翻译一下,就是...

    java7帮助文档

    This release contains Java API for XML Processing (JAXP) 1.4.5, supports Java Architecture for XML Binding (JAXB) 2.2.3, and supports Java API for XML Web Services (JAX-WS) 2.2.4. ...

    21天自学JAVA

    it to create applications for any computing environment and Android apps that run on cell phones and other mobile devices. By the time you have finished the book, you’ll have well-rounded knowledge ...

    解决CXF webService 调用报错 Cannot create a secure XMLInputFactory

    带详细解决方案说明文档,提供三种解决方式,另外包含stax2-api.jar,stax2-api-source.jar,woodstox-core-asl-4.4.1.jar,stax-ex.jar,wstx-asl-3.2.9.jar等jar文件,亲测有效。

    DWR.xml配置文件说明书(含源码)

    &lt;creator id="new" class="uk.ltd.getahead.dwr.create.NewCreator"/&gt;DWR已经将这配置到了内置的dwr.xml文件中,并不需要进行额外的配置. Creator通过调用类的默认的构造方法创建实例.应用new creator有以下几个优点:...

    Pro.Java.8.Programming.3rd.Edition.1484206428

    By the end of the book, you’ll be fully prepared to take advantage of Java's ease of development, and able to create powerful, sophisticated Java applications. What youll learn How to use and ...

    英文原版-Instant Messaging in Java 1st Edition

    * This book focuses on the open source Jabber XML-based IM protocols (www.jabber.org) to create Java IM software. These open protocols allowS your IM Software to seamlessly communicate with the large...

    android 使用poi读取高版本excel

    java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/stream/XMLEventFactory; at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.(PackagePropertiesMarshaller...

    Manning Java Reflection In Action

    the sound conceptual basis that developers need to create advanced applications. The book includes careful explanations of sometimes perplexing programming techniques along with enough background to ...

    Pro.Java.8.Programming.3rd.Edition.1484206428.epub

    By the end of the book, you’ll be fully prepared to take advantage of Java's ease of development, and able to create powerful, sophisticated Java applications. What youll learn How to use and ...

    create_enum_from_xml:从 Android attrs.xml 创建 Java 枚举

    从 Android attrs XML 生成 Java 枚举类介绍(和警告) 这是一个黑客。 而已。 我敢肯定,拥有比我更多 Gradle 技能的人可能可以将它变成一个合适的插件,但是如果你需要一些快速而肮脏的东西,试试这个……但如果它...

    基于java的大数据预测系统(设计)可运行可演示

    2、配置java环境,使用jdk8,配置环境变量,下载IntelliJ IDEA 2019.2.4,该工具为java代码编译器 3、下载Maven,配置至环境变量(百度搜索很多),将构建器为Maven,类库配置成阿里库(方法:百度搜索很多很多) 4...

    java自动建表架包(修正版)

    此架包 flyDB V1.2 beta 为创建简单数据表的架包 ...创建数据库的方法是CreateTable工具类调用静态方法create()即可 创建表的方法是CreateTable工具类调用静态方法start()即可 更多新功能正在修正中...

    JSP and XML Integrating XML and Web Services in JSP

    In the book, you'll learn the technologies and techniques needed to create your own Web services for use in JSP applications. Written by programmers for programmers, the book will help you ...

    java 调用webservice的各种方法总结

    // create and publish an endpoint Hello hello = new Hello(); Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello); } } Java代码 package Hello; import javax.jws....

Global site tag (gtag.js) - Google Analytics