public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
// DocumentBuilderFactory domFactory = DocumentBuilderFactory
// .newInstance();
// domFactory.setNamespaceAware(true); // never forget this!
// DocumentBuilder builder = domFactory.newDocumentBuilder();
// Document doc = builder.parse("c:/solr.xml");
// XPathFactory factory = XPathFactory.newInstance();
// XPath xpath = factory.newXPath();
// Node nd = (Node)xpath.evaluate("solr/cores/@adminPath", doc, XPathConstants.NODE);
// String txt = nd.getNodeValue();
// System.out.println(txt);
//
// //多核循环读取,属性读取
// XPath mulXpath = factory.newXPath();
// Object o = mulXpath.evaluate("solr/cores/core", doc, XPathConstants.NODESET);
// NodeList nodes = (NodeList) o;
// for (int i = 0; i < nodes.getLength(); i++) {
// Node node = nodes.item(i);
// Node aNode =(Node) node.getAttributes().getNamedItem("name");
// System.out.println(aNode.getNodeValue());
// // String opt = DOMUtil.getAttr(node, "name", null);
// // System.out.println(opt);
// System.out.println(node.getNodeName());
// Node eNode = node.getFirstChild();
// System.out.println(eNode.getNodeValue());
// System.out.println(node.getNodeType());
// }
String strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><class><stu><id>1,2,3</id></stu></class>";

DocumentBuilderFactory domFactory = DocumentBuilderFactory
.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
InputStream inputStream = new ByteArrayInputStream(strXml.getBytes());;
Document doc = builder.parse(inputStream);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
//Node nd = (Node)xpath.evaluate("class/stu/@id", doc, XPathConstants.NODE);
//String txt = nd.getNodeValue();
// System.out.println(txt);

//多核循环读取,属性读取
XPath mulXpath = factory.newXPath();
Object o = mulXpath.evaluate("class/stu/id", doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) o;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
// Node aNode =(Node) node.getAttributes().getNamedItem("name");
// System.out.println(aNode.getNodeValue());
// String opt = DOMUtil.getAttr(node, "name", null);
// System.out.println(opt);
System.out.println(node.getNodeName());
Node eNode = node.getFirstChild();
System.out.println(eNode.getNodeValue());
System.out.println(node.getNodeType());
}
}

sax xpath读取xml字符串的更多相关文章

  1. 利用SAX解析读取XML文件

    xml     这是我的第一个BLOG,今天在看<J2EE应用开发详解>一书,书中讲到XML编程,于是就按照书中的步骤自己测试了起来,可是怎么测试都不成功,后来自己查看了一遍源码,发现在读 ...

  2. 利用XPath读取Xml文件

    之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...

  3. XPath读取xml文件

    1.创建解析工厂 2.创建解析器 3.读xml文件,生成w3c.docment对象树 4.创建XPath对象 5.通过路径查找对象 例子: import javax.xml.parsers.Docum ...

  4. sqlserver 读取xml 字符串方法

    declare @xml xml declare @propertyName varchar(50)  declare @str nvarchar(max)   set @propertyName = ...

  5. dom4j处理xml文件,读取xml字符串,格式化xml文件

    1.xml文件 <?xml version="1.0" encoding="UTF-8"?> <employees> <emplo ...

  6. 读取XML字符串到临时表

    DECLARE @hdoc int DECLARE @doc xml SET @doc = '<CityValueSet> <CityItem> <CityId>2 ...

  7. MSSQL读取xml字符串到临时表

    DECLARE @hdoc int DECLARE @doc xml SET @doc ='<CityValueSet> <CityItem> <CityId>20 ...

  8. 读取xml字符串

    string strXml = @"<MessageData><pm_id>10</pm_id><pm_title>这是公司或者产品的名称&l ...

  9. xpath 操作XML

    1.xpath 操作XML,底下部分代码被注释了,但是是完整功能,去除注释是正常使用的(有写命名和其他冲突,所以注释了) 总体有:完整读取xml,对xml的增删改查,对xml的特定操作 using S ...

随机推荐

  1. Windows 7 IE主页被篡改,如何修复?

    有时我们的电脑会因为病毒的入侵,使得IE主页被篡改,然后就会被没底线的广告包围,有时用杀毒软件也不修复,那么此时应该怎么修复呢?其实很简单,只需几步,就可以让您的电脑重新清净下来. 第一步 点击“开始 ...

  2. Codeforces 209 C. Trails and Glades

    Vasya went for a walk in the park. The park has n glades, numbered from 1 to n. There are m trails b ...

  3. vert.x学习(二),使用Router来定义用户访问路径

    这里需要用到vertx-web依赖了,依然是在pom.xml里面导入 <?xml version="1.0" encoding="UTF-8"?> ...

  4. linux下常见的字符串处理

    linux下有许多对字符串文本的处理工具,diff,grep,awk,sed等等.. diff 比较两个文件的不同 [root@bogon diff]# diff file1 file2 以高亮的形式 ...

  5. C# 日期格式转【转】

    使用:DateTime.ToString的方法(String, IFormatProvider)转换格式 using System; using System.Globalization; Strin ...

  6. .net使用pdfobject.js加载pdf文件

    1.下载pdfobject.js文件 2. <script type="text/javascript" src="<%= Application[" ...

  7. Win8电脑蓝屏并提示dpc_watchdog_violation

    用尽系统自带的工具均无法恢复,F8能进系统.后来使用如下方法解决了 这种错误情况的发生可能是由于 iastor.sys 驱动没有完全兼容 Windows 8系统所造成的. 微软正在研究一种可行方案,来 ...

  8. android.graphic.Path

    类path是一个封装的几何学路径包括直线,二次曲线,三次曲线.它可以通过函数canvas.drawPath(path, paint)画出来,可以通过填充方式或者画线方式(由paint的style决定) ...

  9. 如何把Excel中的某列数值如何转换成文本格式,且兼容性最好?

    假设要把下方A列数值变成文本格式.有多种方法,其中下方的方法兼容性最好: 第一步:选择 A 列,然后,运行菜单 数据/分列... 命令,如下图: 第二步:默认 下一步: 第三步:默认 下一步: 第四步 ...

  10. Leetcode: Optimal Account Balancing

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...