sax xpath读取xml字符串
2015-07-26 21:46
357 查看
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());
}
}
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());
}
}
相关文章推荐
- 用IE浏览器打开pdf文件出来的是空白页面,怎么办?
- Swift学习笔记(十一)——Swift中三元运算符的低级错误:Consecutive statements on a line must be separated by';'
- 每天一个小知识点14(Angularjs总结二)
- 《开源框架那点事儿25》:对框架模板引擎实现方式的改造实录
- 关于java中的static和final的总结
- 在Hyper-V虚拟机中使用Wi-Fi上网
- 数据结构之选择排序--简单选择排序
- 五毛的cocos2d-x学习笔记03-控件
- 分布式服务框架:Zookeeper
- (九十)使用多个storyboard+代码实现控制器的分开管理
- EventBus使用详解(二)——EventBus使用进阶
- cocos2dx_lua_v3.7 open console
- 网络工程_java(上)
- 递归和迭代两种方式实现归并排序(Java版)
- (九十)使用多个storyboard+代码实现控制器的分开管理
- 原始的冒泡排序
- ISA2006配置日志存储限制
- 《开源框架那点事儿25》:对框架模板引擎实现方式的改造实录
- 【转】Backbone.js学习笔记(二)细说MVC
- C++ 获取类成员函数地址方法 浅析