DocumentHelper用法
2016-01-06 18:13
183 查看
public static void main(Stringargs[]){
Strings="<?xml version=\"1.0\" encoding=\"gbk\"?>" +
"<smartresult>" +
"<product type=\"identitycard\">" +
"<code>440305198012255411</code>" +
"<location>sdfsdf</location>" +
"<birthday>19801225</birthday>" +
"<gender>m</gender></product>" +
"</smartresult>";
try {
Document doc = DocumentHelper.parseText(s);
Node root=doc.getRootElement();
System.out.println(root.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
一、DocumentHelper类的主要静态方法:
1.parseText
publicstatic Document parseText(String text)
throws DocumentException
parseText parses the given text as an XML document and returns the newlycreated Document.
Parameters:
text - the XML text to be parsed
Returns:
a newly parsed Document
Throws:
DocumentException - if the documentcould not be parsed
二、Document接口的主要方法:
1.getRootElement
publicElement getRootElement()
Returns the root Element for this document.
Returns:
the root element for this document
三、Element接口的主要方法:
1.getText(这个是element接口的方法)
返回当前元素的文本内容,如果元素没有文本内容,则返回空。
publicString getText()
Returns the text value of this element without recursing through childelements. This method iterates through all Text,CDATA and Entitynodes that thiselement contains and
appendsthe text values together.
Specified by:
getText in interface Node
Returns:
the textual content of this Element.Child elements are not navigated. This method does not return null;
2.getName(这个是node接口的方法,由于element继承node,所有,element也可以调用该方法)
publicString getName()
getName returns the name of this node. This is the XML local name of theelement, attribute, entity or processing instruction. For CDATA and Text nodesthis method will
returnnull.
Returns:
the XML name of this node
四、Document类的继承结构
public interface Document
extends Branch
publicinterface Branch
extends Node
Document接口继承Branch,Branch接口又继承Node接口,而Node接口有个方法,经常用到如下:
List<Node>selectNodes(String xpathExpression)
selectNodesevaluates an XPath expression and returns the result as a List of Nodeinstances or String instances depending on the XPath expression.
Parameters:
xpathExpression - is the XPath expression to be evaluated
Returns:
the list of Node or String instances depending on the XPath expression
Strings="<?xml version=\"1.0\" encoding=\"gbk\"?>" +
"<smartresult>" +
"<product type=\"identitycard\">" +
"<code>440305198012255411</code>" +
"<location>sdfsdf</location>" +
"<birthday>19801225</birthday>" +
"<gender>m</gender></product>" +
"</smartresult>";
try {
Document doc = DocumentHelper.parseText(s);
Node root=doc.getRootElement();
System.out.println(root.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
一、DocumentHelper类的主要静态方法:
1.parseText
publicstatic Document parseText(String text)
throws DocumentException
parseText parses the given text as an XML document and returns the newlycreated Document.
Parameters:
text - the XML text to be parsed
Returns:
a newly parsed Document
Throws:
DocumentException - if the documentcould not be parsed
二、Document接口的主要方法:
1.getRootElement
publicElement getRootElement()
Returns the root Element for this document.
Returns:
the root element for this document
三、Element接口的主要方法:
1.getText(这个是element接口的方法)
返回当前元素的文本内容,如果元素没有文本内容,则返回空。
publicString getText()
Returns the text value of this element without recursing through childelements. This method iterates through all Text,CDATA and Entitynodes that thiselement contains and
appendsthe text values together.
Specified by:
getText in interface Node
Returns:
the textual content of this Element.Child elements are not navigated. This method does not return null;
2.getName(这个是node接口的方法,由于element继承node,所有,element也可以调用该方法)
publicString getName()
getName returns the name of this node. This is the XML local name of theelement, attribute, entity or processing instruction. For CDATA and Text nodesthis method will
returnnull.
Returns:
the XML name of this node
四、Document类的继承结构
public interface Document
extends Branch
publicinterface Branch
extends Node
Document接口继承Branch,Branch接口又继承Node接口,而Node接口有个方法,经常用到如下:
List<Node>selectNodes(String xpathExpression)
selectNodesevaluates an XPath expression and returns the result as a List of Nodeinstances or String instances depending on the XPath expression.
Parameters:
xpathExpression - is the XPath expression to be evaluated
Returns:
the list of Node or String instances depending on the XPath expression
相关文章推荐
- freemarker中的list 前端模板
- IE主页被恶意修改处理办法
- ipc之消息队列
- C++ primer plus读书笔记——第13章 类继承
- javascript清空所有input的值
- c++例子分析-内存内容覆盖
- java实现坦克大战3.1版
- 什么是DirectX,总是看到显卡支持DirecTX多少
- Eclipse 搭建 Spring+SpringMVC+Mybatis(1)
- 本地连接批处理修改IP
- C++ primer plus读书笔记——第12章 类和动态内存分配
- linux C语言实现文件锁
- Html5添加Tabs样式单页多图轮播图插件教程
- RestTemplate远程接口调用
- C++ primer plus读书笔记——第11章 使用类
- linux性能分析工具
- 命令行下mysql新建用户及分配权限
- java 实现坦克大战 3.0版
- 合成
- java Socket