DocumentHelper用法
2013-11-12 00:52
435 查看
注意: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
相关文章推荐
- 怎样学习java,漫谈java学习之路
- java基础知识笔记
- java中web应用中重定向与转发的区别
- 一个简单的加密解密程序
- Java中基础数据类型
- java基础之IO操作
- javaIO中FileInputStream,FileReader等的区别
- 更精确地进行浮点数计算
- java 笔记
- java boxing
- 黑马程序员---java基础知识
- 黑马程序员 IO流-->字符流
- 黑马程序员 IO流-->字节流
- JAVA外部比较器:实现Comparator接口
- 黑马程序员-----java编程基础常识
- Java数据类型的划分
- Java程序中的注解、标示符、关键字、变量和常量
- Java开发环境和搭建
- 什么是Java?
- java基础(一)