视窗
loading...
您现在的位置:首页 > IT认证 > JAVA >

java中xml解析器的应用实例


java中xml解析器的应用实例

 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;

    /**

    *

Title:

    *

    *

Description:

    *

    *

Copyright: Copyright (c) 2005

    *

    *

Company:

    *

    * @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();

    }

    }

2、jdom

package org.lyj.xml;

import org.jdom.output.Format;

import org.jdom.Document;

import java.io.IOException;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

import org.jdom.JDOMException;

/**

*

Title:


*

*

Description:


*

*

Copyright: Copyright (c) 2005


*

*

Company:


*

* @author not attributable

* @version 1.0

*/

public class JdomWriter {

public JdomWriter() {

}

public void write(){

try {

SAXBuilder builder = new SAXBuilder();

Document doc = builder.build("aa.xml");

XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()。setEncoding("utf-8"));

outputter.output(doc, new java.io.FileOutputStream("bb.xml"));

} catch (JDOMException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String args[]){

JdomWriter jd = new JdomWriter();

jd.write();

}

}

銆€

銆€

銆愬厤璐e0鏄庡拰鐗堟潈璇存槑銆�
鏈珯閮ㄥ垎淇℃伅鏉ユ簮浜庝簰鑱旂綉锛�瀵硅浆杞界殑淇℃伅鎴戜滑鍔涙眰鏍囨槑淇℃伅鐨勫嚭澶勶紝鎴戜滑灏婇噸浣滆€呯殑鐗堟潈钁椾綔鏉冿紝瀵规垜浠浆杞界殑淇℃伅濡傛湁寮傝鎴栨湁渚垫潈鎴栬繚娉曚俊鎭鍙婃椂鑱旂郴鎴戜滑锛屾湰绔欎細绔嬪嵆鍒犻櫎锛�瀵瑰師鍒涘唴瀹规垜浠効鎰忔敮浠橀€傚綋鐨勭ǹ閰紝鑱旂郴鏂瑰紡锛氶偖浠�:webmaster@jscj.com銆€鐢佃瘽锛�4008816886

相关文章

无相关信息
更新时间2022-03-13 11:11:04【至顶部↑】
联系我们 | 邮件: webmaster@jscj.com | 客服热线电话:4008816886(QQ同号) |  娣诲姞瀹㈡湇寰俊

付款方式留言簿投诉中心网站纠错二维码手机版

客服电话: