【转】+【改】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")
相关文档:
http://www.cnblogs.com/long2006sky/articles/1258731.html
DataSet转换为xml文件
//将DataSet转换为xml文件
private static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
{
&n ......
using System;
using System.Xml;
namespace ReadXMLfromFile
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("books.xml");
......
// MsXmlTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "MsXmlTest.h"
#include <clocale>
#include "comutil.h"
#import "msxml4.dll"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
void WritePerson(MSXML2: ......
package test;
import java.util.ArrayList;
import java.util.List;
import org.nuxeo.common.xmap.annotation.XNode;
import org.nuxeo.common.xmap.annotation.XNodeList;
import org.nuxeo.common.xmap.annotation.XObject;
/**
* Book 实体对象,此处用XMap注解
* @author Administra ......