• jdom解析XML 中文


    public void build() throws HsException {
            try {
                SAXBuilder sb = new SAXBuilder();
                String xml = this.headDtd+this.rootDtd;
                debug(xml);
                doc = sb.build(new StringReader(xml));
                root = doc.getRootElement();
            } catch (Exception e) {
                throw new HsException(HsErrorMsg.FILE_ERROR, "默认创建xml文档失败:"
                        + e.getMessage(), e);
            }
        }
    
        /**
         * 把xml结构的文件初始化到JdomXml
         * @param 文件对象
         * @return
         */
        public void build(File file) throws HsException {
            try {
                SAXBuilder sb = new SAXBuilder();
                doc = sb.build(file);
                root = doc.getRootElement();
            } catch (Exception e) {
                throw new HsException(HsErrorMsg.FILE_ERROR, "读取文件,创建xml文档失败:"
                        + e.getMessage(), e);
            }
        }
    
        /**
         * 把字节流的xml结构初始化到JdomXml对象中
         * @param InputStream字节流
         * @return
         */
        public void build(InputStream in) throws HsException {
            try {
                SAXBuilder sb = new SAXBuilder();
                doc = sb.build(in);
                root = doc.getRootElement();
            } catch (Exception e) {
                throw new HsException(HsErrorMsg.FILE_ERROR, "读取IO字节流,创建xml文档失败:"
                        + e.getMessage(), e);
            }
        }
    
        /**
         * 把字符流的xml结构初始化到JdomXml对象中
         * @param Reader字符流
         * @return
         */
        public void build(Reader reader) throws HsException {
            try {
                SAXBuilder sb = new SAXBuilder();
                doc = sb.build(reader);
                root = doc.getRootElement();
            } catch (Exception e) {
                throw new HsException(HsErrorMsg.FILE_ERROR, "读取IO字符流,创建xml文档失败:"
                        + e.getMessage(), e);
            }
        }
    
        /**
         * 把xml结构的字符串初始化到JdomXml对象中
         * @param xml的字符串
         * @return
         */
        public void build(String strDoc) throws HsException {
            try {
                SAXBuilder sb = new SAXBuilder();
                doc = sb.build(new StringReader(strDoc));
                root = doc.getRootElement();
            } catch (Exception e) {
                throw new HsException(HsErrorMsg.FILE_ERROR, "字符串初始化,创建xml文档失败:"
                        + e.getMessage(), e);
            }
        }
    package com.xml.jdom.test;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    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 DOMTest {
        private String inFile = "d:\\people.xml";
        private String outFile = "d:\\people.xml";
        public static void main(String[] args){
            new DOMTest();
        }
        public DOMTest(){
            try{
                DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document doc = builder.newDocument();
                Element root = doc.createElement("老师");
                Element wang = doc.createElement("王");
                Element liu = doc.createElement("刘");
                wang.appendChild(doc.createTextNode("我是王老师"));
                root.appendChild(wang);
                doc.appendChild(root);
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.transform(new DOMSource(doc), new StreamResult(outFile));
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
    }
    <?xml version="1.0" endoding="gb2312" ?>
    <person>
        <name>王小明</name>
        <college>计算机科学与技术学院</college>
        <telephone>666888</telephone>
        <notes>男,1989年生</notes>
    </person>
    package com.xml.test;
    
    import java.util.Hashtable;
    
    import org.xml.sax.*;
    
    public class SAXHandler extends HandlerBase{
        private Hashtable table = new Hashtable();
        private String currentElement = null;
        private String currentValue= null;
        public void setTable(Hashtable table){
            this.table = table;
        }
        public Hashtable getTable(){
            return table;
        }
        public void startElement(String tag,AttributeList attrs) throws SAXException{
            currentElement = tag;
        }
        public void characters(char[] ch,int start,int length)throws SAXException{
            currentValue = new String(ch,start,length);
        }
        public void endElement(String name)throws SAXException{
            if(currentElement.equals(name))
                table.put(currentElement, currentValue);
        }
        
    }
    <%@ page language="java" import="java.util.*" pageEncoding="GB2312" contentType="text/html;charset=GB2312"%>
    <%//@ page errorPage="ErrPage.jsp"%>
    <%@ page import="java.io.*" %>
    <%@ page import="java.util.Hashtable" %>
    <%@ page import="org.w3c.dom.*" %>
    <%@ page import="org.xml.sax.*" %>
    <%@ page import="javax.xml.parsers.SAXParserFactory" %>
    <%@ page import="javax.xml.parsers.SAXParser" %>
    <%@ page import="com.xml.test.SAXHandler" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>剖析XML文件people.xml</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <% 
            String filename = "d:\\people.xml";
            File file = new File(filename); 
            FileReader reader = new FileReader(file);
            Parser parser;
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            SAXHandler handler = new SAXHandler();
            sp.parse(new InputSource(reader),handler);
            Hashtable hashTable = handler.getTable();
            
        %>
        <table border=2>
            <caption>信息表</caption>
            <tr>
                <td>姓名</td>
                <td><%= (String)hashTable.get(new String("name")) %></td>
            </tr>
            <tr>
                <td>学院</td>
                <td><%= (String)hashTable.get(new String("college")) %></td>
            </tr>
            <tr>
                <td>电话</td>
                <td><%= (String)hashTable.get(new String("telephone")) %></td>
            </tr>
            <tr>
                <td>备注</td>
                <td><%= (String)hashTable.get(new String("notes")) %></td>
            </tr>
        </table>
      </body> 
    </html>
  • 相关阅读:
    P4555 [国家集训队]最长双回文串(回文树)
    【洛谷 P3805】 【模板】manacher算法
    【洛谷 P2485】 [SDOI2011]计算器 (BSGS)
    【洛谷 P3846】 [TJOI2007]可爱的质数 (BSGS)
    【洛谷 P1712】 [NOI2016]区间 (线段树+尺取)
    【洛谷 P1251】 餐巾计划问题 (费用流)
    【洛谷 P1337】[JSOI2004]平衡点 / 吊打XXX (模拟退火)
    【POJ 1719】 Shooting Contest (二分图匹配)
    【洛谷 P1631】 序列合并 (堆)
    【洛谷 P2515】 [HAOI2010]软件安装 (缩点+树形背包)
  • 原文地址:https://www.cnblogs.com/mingforyou/p/2976847.html
Copyright © 2020-2023  润新知