【转】+【改】DOM4J处理XML带有命名空间的四种方法
当你解析XML时,是否会因为命名空间的存在而不能得偿所愿呢?
java方面,好多人推荐用dom4j处理xml,我也就说说在dom4j上处理带命名空间的xml
xml代码example: 再说前三种方法,也是从网上看来的。http://www.cnblogs.com/patrickchen/articles/1188920.html
D: eport.css
第一个方案.设置你的xpath的命名空间setNamespaceURIs
1: public class TransferXML { 2: public static void main(String[] args) throws Exception{ 3: Map map = new HashMap(); 4: map.put("design","http://www.eclipse.org/birt/2005/design"); 5: SAXReader saxReader = new SAXReader(); 6: File file = new File("D:\test.xml"); 7: Document document = saxReader.read(file); 8: XPath x = document.createXPath("//design:list-property"); 9: x.setNamespaceURIs(map); 10: List nodelist = x.selectNodes(document); 11: System.out.println(nodelist.size()); 12: } 13: }
第二个解决方案:设置你的DocumentFactory()的命名空间 setXPathNamespaceURIs
1: public class TransferXML { 2: public static void main(String[] args) throws Exception{ 3: Map map = new HashMap(); 4: map.put("design","http://www.eclipse.org/birt/2005/design"); 5: SAXReader saxReader = new SAXReader(); 6: File file = new File("D:\test.xml"); 7: saxReader.getDocumentFactory().setXPathNamespaceURIs(map); 8: Document document = saxReader.read(file); 9: List tmp = document.selectNodes("//design:list-property"); 10: System.out.println(tmp.size()); 11: } 12: }
第三种方法:本人用的,最笨也是最通用的方法,就是不使用开发环境给你提供的一系列对象,而是用XPath语法中自带的local-name() 和 namespace-uri() 指定你要使用的节点名和命名空间。
当你遇到使用xslt来样式化xml时,就知道这个笨方法的好处了:
1: public class TransferXML { 2: public static void main(String[] args) throws Exception 3: SAXReader saxReader = new SAXReader(); 4: File file = new File("D:\test.xml")
相关文档:
方法一:
采取通用的base64编码方式,取时解码存时加码。
毛老师提供了完整的编码代码,且效率很高。
unit Base64;
interface
uses SysUtils, Classes;
type
{$IFDEF UNICODE}
Base64String = AnsiString;
{$ELSE}
Base64String = strin ......
Jdom
读取
xml
文件例子
1.
用
JDOM
读取
XML
文件需先用
org.jdom.input.SAXBuilder
对象的
build()
方法创建
Document
对象
,
然后用
Document
类、
Element
类等的方法读取
1.
Sample1.java
import
java.util.*;
import
org.j ......
/*
数据库查询XML结构,FOR XML PATH 语句的应用
*/
FOR XML PATH 语句的应用:
CREATE TABLE TempTable(UserID int , UserName nvarchar(50));
insert into TempTable (UserID,UserName) values (1,'a')
insert into TempTable (UserID,UserName) values (2,'b')
select UserID,UserName from TempTable FOR ......
Integration with the XML Data Type
With the introduction of the XML data type, we wanted to also give FOR XML the ability to generate an instance of XML directly (more precisely, it generates a single row, single column rowset where the cell contains the XML data type instance).
Because of the bac ......