documentHelper的理解与使用
2015-06-18 13:47
399 查看
注意:Node是element的父类。
public static void main(String args[]){
String s="<?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
public static Document parseText(String text)
throws DocumentException
parseText parses the given text as an XML document and returns the newly created Document.
Parameters:
text - the XML text to be parsed
Returns:
a newly parsed Document
Throws:
DocumentException - if the document could not be parsed
二、Document接口的主要方法:
1.getRootElement
public Element getRootElement()
Returns the root Element for this document.
Returns:
the root element for this document
三、Element接口的主要方法:
1.getText(这个是element接口的方法)
返回当前元素的文本内容,如果元素没有文本内容,则返回空。
public String getText()
Returns the text value of this element without recursing through child elements. This method iterates through all Text,CDATA and Entitynodes that this element contains and
appends the 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也可以调用该方法)
public String getName()
getName returns the name of this node. This is the XML local name of the element, attribute, entity or processing instruction. For CDATA and Text nodes this method will
return null.
Returns:
the XML name of this node
四、Document类的继承结构
public interface Document
extends Branch
public interface Branch
extends Node
Document接口继承Branch,Branch接口又继承Node接口,而Node接口有个方法,经常用到如下:
List<Node> selectNodes(String xpathExpression)
selectNodes evaluates an XPath expression and returns the result as a List of Node instances 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
public static void main(String args[]){
String s="<?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
public static Document parseText(String text)
throws DocumentException
parseText parses the given text as an XML document and returns the newly created Document.
Parameters:
text - the XML text to be parsed
Returns:
a newly parsed Document
Throws:
DocumentException - if the document could not be parsed
二、Document接口的主要方法:
1.getRootElement
public Element getRootElement()
Returns the root Element for this document.
Returns:
the root element for this document
三、Element接口的主要方法:
1.getText(这个是element接口的方法)
返回当前元素的文本内容,如果元素没有文本内容,则返回空。
public String getText()
Returns the text value of this element without recursing through child elements. This method iterates through all Text,CDATA and Entitynodes that this element contains and
appends the 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也可以调用该方法)
public String getName()
getName returns the name of this node. This is the XML local name of the element, attribute, entity or processing instruction. For CDATA and Text nodes this method will
return null.
Returns:
the XML name of this node
四、Document类的继承结构
public interface Document
extends Branch
public interface Branch
extends Node
Document接口继承Branch,Branch接口又继承Node接口,而Node接口有个方法,经常用到如下:
List<Node> selectNodes(String xpathExpression)
selectNodes evaluates an XPath expression and returns the result as a List of Node instances 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
相关文章推荐
- js读取php设置的cookie乱码 中文
- 配色弱的进来!如何巧用色彩打造动人心弦的网页设计
- TCP和UDP的区别(转)
- 24小时制转12小时制
- 在改版的Spring官网下载所需的jar包
- VS2010每次编译都重新编译整个工程的解决方案
- 纯CSS绘制三角形
- 第11.12章
- 他山之石
- openwrt 安装ssh客户端更新openwrt安装源
- Google I/O 2013:Volley 图片缓存教程
- Xilinx FPGA ISE JTAG boundary scan 扫描出 unknown device的问题
- 黑马day04 凤姐艺妓录&防盗链
- Sublime Text 3 安装Package Control官方教程
- javascript:让表单 文本框 只读,不可编辑的方法
- 从assets中把zip资源包复制到SD卡中
- 灰度图转伪彩色图
- 我曾经面试过这样一个问题
- 快捷键让SublimeText在编文件快速在浏览器打开
- 今日之谬言,不谦谓悟道