胖熊熊

          得到多少又失去了什么

          【轉】解析xml文件獲取encoding的值

          轉自http://faq.csdn.net/read/207889.html

          為 DOM Level 3 而完成的一項重要任務是:通過加入新的可以查詢缺少的 XMLInfoset 信息的方法,使 DOM 數據模型與 XML Information Set(Infoset)相匹配。例如,現在可以通過 Document 接口(它被映射到 Infoset 文檔信息項)查詢和修改儲存在一個 XML 聲明中的信息,例如 version、standalone 和 encoding。類似地,基本 URI 和聲明基本 URI 屬性是根據 XML Base 處理的,它們被放在 Node 接口中。您還可以獲取 XML Infoset 元素內容的 whitespace 屬性。這個屬性表明一個 Text 節點是否只包含可以被忽略的空白。可以通過 Text 接口(它映射到 XML Inforset 字符信息項)獲得這個屬性。清單1展示了在 Java 語言綁定中這個接口中的實際方法簽名。

          清單1. 在 Java 語言綁定的方法簽名

          // XML Declaration information on
          // the org.w3c.dom.Document interface
          public String getXmlEncoding();
          public void setXmlEncoding(String xmlEncoding);
          public boolean getXmlStandalone();
          public void setXmlStandalone(boolean xmlStandalone)
                                            throws DOMException;
          public String getXmlVersion();
          public void setXmlVersion(String xmlVersion)
                                            throws DOMException;

          // element content whitespace property on the Text 
          // interface
          public boolean isWhitespaceInElementContent();

           

          通過 Attr 接口的 schemaTypeInfo 屬性,您還可以獲取一個屬性信息項的屬性類型特性的值 ——即一個屬性的類型。后面有一節對此給予了更詳細的介紹。

          此外,這里提供了一個新的特性,用于以最接近 XML Infoset 的形式返回 Document,在此之前,由于不同的編輯操作(例如插入或者刪除節點)的作用,文檔通常會更加偏離 XML Infoset。這是在進行文檔標準化(document normalization)操作時可能造成的部分結果,我們將在下面的文檔標準化一節中對此加以描述。

          最后,新的 Appendix C 提供了 XML Infoset 模型與 DOM 之間的映射,在這種映射中,每一個 XML Infoset 信息項都映射到其相應的 Node,反之也一樣,一個信息項的每一個屬性都映射到其相應 Node 的屬性。這個附錄應該可以使您對 DOM 數據模型有一個很好的全面了解,并且展示了如何訪問所要查找的信息。


          ---------------------------------------------------------------

          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder builder = factory.newDocumentBuilder();
                  InputStream in = Test.class.getResourceAsStream(fileName);
                  DocumentImpl doc = (DocumentImpl) builder.parse(in);

          ---------------------------------------------------------------

          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;

          import org.apache.xerces.dom.DocumentImpl;

                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                      DocumentBuilder builder = factory.newDocumentBuilder();
                      InputStream in = new FileInputStream(args[0]);
                      DocumentImpl doc = (DocumentImpl)builder.parse(in);
                      System.out.println(doc.getXmlEncoding());

          ============================================================================
          import org.dom4j.Document;
          import org.dom4j.DocumentHelper;


                      String xml = "<?xml version='1.0' encoding='iso-8859-1'?><Message>Hi there</Message>";
                      Document doc = DocumentHelper.parseText(xml);
                      System.out.println("The encoding is " + doc.getXMLEncoding());
                      System.out.println("As XML: " + doc.asXML());

          The result is:

          The encoding is iso-8859-1
          As XML: <?xml version="1.0" encoding="iso-8859-1"?>
          <Message>Hi there</Message>

          =================================

                      String xml = "<?xml version='1.0' encoding='UTF-8'?><Message>Hi there</Message>";
                      Document doc = DocumentHelper.parseText(xml);
                      System.out.println("The encoding is " + doc.getXMLEncoding());
                      System.out.println("As XML: " + doc.asXML());

          The result is:

          The encoding is UTF-8

          As XML: <?xml version="1.0" encoding="UTF-8"?>
          <Message>Hi there</Message>

          ====================================
                      String xml = "<?xml version='1.0' encoding='GBK'?><Message>Hi there</Message>";
                      Document doc = DocumentHelper.parseText(xml);
                      System.out.println("The encoding is " + doc.getXMLEncoding());
                      System.out.println("As XML: " + doc.asXML());

          The result is:

          The encoding is GBK

          As XML: <?xml version="1.0" encoding="GBK"?>
          <Message>Hi there</Message>

          posted on 2008-11-13 10:33 fatbear 閱讀(1794) 評論(0)  編輯  收藏 所屬分類: XML/XSLT


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 林西县| 石景山区| 定州市| 平舆县| 栾城县| 红原县| 闽侯县| 康定县| 云林县| 磐安县| 新宁县| 广河县| 涞水县| 嘉善县| 宜昌市| 公安县| 庆元县| 尉氏县| 丹棱县| 赤壁市| 新密市| 偃师市| 灌云县| 泽库县| 峨眉山市| 界首市| 古交市| 喀什市| 枞阳县| 和政县| 桂阳县| 英吉沙县| 自贡市| 淮安市| 天柱县| 四平市| 桐柏县| 黎平县| 广平县| 射洪县| 华池县|