你所应该知的Dom4J.docVIP

  • 8
  • 0
  • 约5.82千字
  • 约 4页
  • 2016-12-03 发布于贵州
  • 举报
你所应该知的Dom4J

你所应该知道的Dom4J 创建解析器: SAXReader reader = new SAXReader(); 利用解析器读入xml文档: Document?? document = reader.read(new File(input.xml)); 获取文档的根节点: Element root = document.getRootElement(); 接口继承结构: Node --- Branch --Document --Element ---- Attribute Node接口 String asXML() asXMLreturns the textual XML representation of this node. 将一个节点转换为字符串 String getName() getNamereturns the name of this node. 获取节点的名称,如果是元素则获取到元素名,如果是属性获取到属性名 short getNodeType() Returns the code according to the type of node. 获取节点类型,在Node接口上定义了一些静态short类型的常量用来表示各种类型 Element getParent() getParentreturns the parent Element if this node supports the parent relationship or null if it is the root element or does not support the parent relationship. 获取父节点,如果是根元素调用则返回null,如果是其他元素调用则返回父元素,如果是属性调用则返回属性所依附的元素。 String getText() Returns the text of this node. 返回节点文本,如果是元素则返回标签体,如果是属性则返回属性值 List selectNodes(String xpathExpression) selectNodesevaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression. 利用xpath表达式,选择节点 void setName(String name) Sets the text data of this node or this method will throw an UnsupportedOperationException if it is read-only. 设置节点的名称,元素可以更改名称,属性则不可以,会抛出UnsupportedOperationException 异常 void setText(String text) Sets the text data of this node or this method will throw an UnsupportedOperationException if it is read-only. 设置节点内容,如果是元素则设置标签体,如果是属性则设置属性的值 void write(Writer writer) writewrites this node as the default XML notation for this node. 将节点写出到一个输出流中,元素、属性均支持 Branch接口(实现了Node接口) void add(Element element) Adds the given Element to this branch. 增加一个子节点 Element addElement(QName qname) Adds a new Element node with the given QNameto this branch and returns a reference to the new node. 增加一个给定名字的子节点,并且返回这个新创建的节点的引用 int indexOf(Node node) Returns the index of the given node if it is a child node of this branch or -1 if the given node is not a child node. 获取给定节点在所有直接点中的位置号,如果该节点不是此分支的子节点,则返回-1 boolean remove(E

文档评论(0)

1亿VIP精品文档

相关文档