技术中心

这里象征着我们的态度和能力

>java中xml解析器的应用实例
作者:中国IT实验室    来源:中国IT实验室    发布时间:2012-03-12      浏览次数:6802
分享到:
欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

    xml解析中,通常大家读文件,解析的时候没有问题,但是在写文件的时候常常会碰到乱码等问题,就我的个人体会是一般只要在写文件的时候采用文件流的形式,设置好当前的编码方式,通常就会避免此类问题的发生!下面有两个简单的分别是采用jdom和dom4j实现的例子:

    1、dom4j

    package org.lyj.xml;

    import java.io.OutputStream;

    import java.io.UnsupportedEncodingException;

    import org.dom4j.Document;

    import org.dom4j.Element;

    import java.io.FileWriter;

    import org.dom4j.io.OutputFormat;

    import org.dom4j.Namespace;

    import org.dom4j.io.XMLWriter;

    import java.io.File;

    import java.io.IOException;

    import org.dom4j.DocumentHelper;

    /**

    * <p>Title: </p>

    *

    * <p>Description: </p>

    *

    * <p>Copyright: Copyright (c) 2005</p>

    *

    * <p>Company: </p>

    *

    * @author not attributable

    * @version 1.0

    */

    public class Dom4jTest {

    public void generateDocument() {

    org.dom4j.DocumentFactory df = new org.dom4j.DocumentFactory();

    Document document = df.createDocument();//.createDocument();

    Element catalogElement = document.addElement("catalog");

    catalogElement.addComment("An XML Catalog");

    catalogElement.addProcessingInstruction("target", "text");

    Element journalElement = catalogElement.addElement("journal");

    journalElement.addAttribute("title", "XML Zone");

    journalElement.addAttribute("publisher", "IBM developerWorks");

    Element articleElement = journalElement.addElement("article");

    articleElement.addAttribute("level", "Intermediate");

    articleElement.addAttribute("date", "December-2001");

    Element titleElement = articleElement.addElement("title");

    titleElement.setText("Java configuration with XML Schema");

    Element authorElement = articleElement.addElement("author");

    Element firstNameElement = authorElement.addElement("录音机");

    firstNameElement.setText("Marcello");

    Element lastNameElement = authorElement.addElement("刘");

    lastNameElement.setText("Vitaletti");

    document.addDocType("catalog",

    null, "file://c:/Dtds/catalog.dtd");

    document.setXMLEncoding("gb2312");

    try {

    OutputFormat of = OutputFormat.createCompactFormat();

    of.setEncoding("gb2312");

    XMLWriter output = new XMLWriter(

    new java.io.FileOutputStream(new File("c:/catalog/catalog.xml")),of);

    output.write(document);

    output.close();

    } catch (IOException e) {

    System.out.println(e.getMessage());

    }

    }

    public static void main(String[] argv) {

    Dom4jTest dom4j = new Dom4jTest();

    dom4j.generateDocument();

    }

    }

[1] [2] 下一页

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号 增值电信业务经营许可证号:滇B2-20110045
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730