对于带有表空间xmlns的xml文件的解析
对于带有表空间xmlns的xml文件的解析,用正常解析文件的方法总是失效,不起作用,无法获得元素。
下面给出两种方法解析此类文件:
1.按正常解析xml文件的方法,需要注意几点:
获取元素Element,不可使用函数:document.selectNodes("//region");
只可以先取到根元素,一级一级往下取,eg:
Element root = document.getRootElement();
Element ele = root.element("head");
获取属性值,可以按一般的方法操作,eg:
List ll = document.selectNodes("//@regionName");
System.out.println("ll.size=" + ll.size());
2.使用XPath。eg:
public void testHasNameSpace(File file) {
SAXReader saxReader = new SAXReader();
Document document = null;
// XmlDocument document=
try {
document = saxReader.read(file);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap xmlMap = new HashMap();
xmlMap.put("smil", "http://www.w3.org/2000/SMIL20/CR/Language");
XPath x = document.createXPath("//smil:region");
x.setNamespaceURIs(xmlMap);
List regionList = x.selectNodes(document);
System.out.println("there are " + regionList.size() + " regions");
XPath att = document.createXPath("//smil:region/@regionName");
att.setNamespaceURIs(xmlMap);
List attrList = att.selectNodes(document);
System.out.println("there are " + attrList.size() + " attrs");
int i = 0;
Iterator it = attrList.iterator();
while (it.hasNext()) {
Attribute a = (Attribute) it.next();
System.out.println((i++) + "个:" + a.getValue());
}
}
相关文档:
关于.apk 文件解压后反编译方法:[仅layout package下的xml 文件]
使用AXMLPrinter将其转换为可读的xml文件:
命令如下:
java -jar AXMLPrinter2.jar main.xml > new_main.xml
AXMLPrinter2.jar工具下载地址:http://code.google.com/p/android4me/downloads/list ......
/*--存为XML
将表/查询存储为标准的XML文件
--*/
/*--调用示例
--用SQL用winows身份验证的情况
exec p_savexml @sql='地区资料',@fname='c:\地区资料.xml'
--用指定的用户
exec p_savexml @sql='地区资料',@fname='c:\地区资料.xml',@userid='sa'
--*/
if exists (select * from db ......
XMLDocument类提供了丰富的属性和方法,可以帮助我们轻松完成xml的编辑。但是,完成后的xml很多情况下可能还是需要以字符串形式传递。XMLDocument有个Save方法,不仅可以保存XML文件至磁盘,还能将其保存至指定的流,然后,就能从这个流(stream)读取所需要的字符串了。
XmlDocument doc = new XmlDocument();
...
.../ ......
方法一 :使用XML控件
<% @ Page Language="C#"%>
<html>
<body>
<h3><font face="Verdana">读取XML方法一</font></h3>
<from runat=server>
<asp:Xml id="xml1" DocumentSource="grade.xml" runat="server" />
</from>
</body>
</ ......
//******************** 头文件 Markup.h *******************
// Markup.h: interface for the CMarkup class.
//
// Markup Release 11.2
// Copyright (C) 2009 First Objective Software, Inc. All rights reserved
// Go to www.firstobject.com for the latest CMarkup and EDOM documentation
// ......