`
747017186
  • 浏览: 316214 次
社区版块
存档分类
最新评论

dom4j里面elements()和contents()方法的区别

    博客分类:
  • xml
 
阅读更多

在dom4j里面这两个方法都返回的是list,但是两个list可能有所不同。看了例子:

xml文件:

<?xml version="1.0"?>

<note>

<to></to>

<from>John</from>

<heading>Reminder</heading>

<body>5555

<head>333</head>

</body>

</note>

 

java文件:

File file=new File("C:\\Users\\Administrator\\Desktop\\zzz\\1.xml");

SAXReader reader=new SAXReader();

Document document=reader.read(file);

 

System.out.println(document.getRootElement().elements());//取到该元素下的子节点,对于孙子节点取不到

System.out.println(document.getRootElement().elements().size());//有多少个直接子节点

 

System.out.println(document.getRootElement().content());//可以取到该元素下所有的后代元素,其中还包括后代元素的text内容

System.out.println(document.getRootElement().content().size());//有多少个后代元素,包括他们text内容的总个数

 

执行结果:

[org.dom4j.tree.DefaultElement@19fcc69 [Element: <to attributes: []/>], org.dom4j.tree.DefaultElement@253498 [Element: <from attributes: []/>], org.dom4j.tree.DefaultElement@9fef6f [Element: <heading attributes: []/>], org.dom4j.tree.DefaultElement@209f4e [Element: <body attributes: []/>]]

4

----------------------------------------------------------------------------------------------

[org.dom4j.tree.DefaultText@17172ea [Text: "

"], org.dom4j.tree.DefaultElement@19fcc69 [Element: <to attributes: []/>], org.dom4j.tree.DefaultText@12f6684 [Text: "

"], org.dom4j.tree.DefaultElement@253498 [Element: <from attributes: []/>], org.dom4j.tree.DefaultText@f38798 [Text: "

"], org.dom4j.tree.DefaultElement@9fef6f [Element: <heading attributes: []/>], org.dom4j.tree.DefaultText@4b222f [Text: "

"], org.dom4j.tree.DefaultElement@209f4e [Element: <body attributes: []/>], org.dom4j.tree.DefaultText@b169f8 [Text: "

"]]

9

 

分享到:
评论

相关推荐

    jQuery.in.Action.3rd.Edition.161729207

    Table of Contents Part 1 Starting with jQuery Chapter 1 Introducing jQuery Part 2 Core jQuery Chapter 2 Selecting elements Chapter 3 Operating on a jQuery collection Chapter 4 Working with properties...

    jQuery1.2API

    $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of...

    Jquery Api

    $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of...

    jQuery.Essentials.

    Chapter 4. Events Chapter 5. Making Your Site Snazzy with jQuery Chapter 6. Better Forms with jQuery Chapter 7. Talking to Your Server Chapter 8. Writing Code that You can Read Later Chapter 9. Faster...

    JavaScript权威指南第五版【新】(犀牛书)

    本书还包括详细的参考手册,涵盖了JavaScript的核心API、遗留的客户端API和W3C标准DOM API,记述了这些API中的每一个JavaScript对象、方法、性质、构造函数、常量和事件处理程序。 这本最畅销的JavaScript参考书的第...

    Getting.Started.with.Polymer.178588

    Chapter 4: Polymer Elements Chapter 5: First Application with Polymer Chapter 6: Polymer Designer Tool and Polymer Starter Kit Chapter 7: Working with Polymer.dart Chapter 8: Best Practices

    D3.js 4.x Data Visualization - Third Edition

    D3.js 4.x Data Visualization - Third Edition by Andrew Rininsland English | 28 Apr. 2017 | ASIN: B01MG90SSJ | 308 Pages | AZW3 | 6.6 MB Key Features Build interactive and rich graphics and ...

    jQuery完全实例.rar

    $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of...

    jQuety1.3中文说明

    $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of...

    Beyond.jQuery.1484222342

    Table of Contents Chapter 1: The Oppressive Magic of jQuery Chapter 2: You Don’t Need jQuery (Anymore) Chapter 3: Understanding the Web API and “Vanilla” JavaScript Chapter 4: Finding HTML Elements...

    Beginning.JavaScript.5th.Edition

    Chapter 4: Functions And Scope Chapter 5: Javascript-An Object-Based Language Chapter 6: String Manipulation Chapter 7: Date, Time, And Timers Chapter 8: Programming The Browser Chapter 9: Dom ...

    精通jquery 2.0(2013年10月版)

    Chapter 4: JavaScript Primer 61 Chapter 5: jQuery Basics 93 Chapter 6: Managing the Element Selection 117 Chapter 7: Manipulating the DOM 145 Chapter 8: Manipulating Elements 177 Chapter 9: ...

    Learning React [2016]下载

    Chapter 12 Accessing DOM Elements Chapter 13 Creating a Single-Page App Using React Router Chapter 14 Building a Todo List App Chapter 15 Setting Up Your React Development Environment Chapter 16 The...

    JavaScript权威指南(第五版).chm

    Part IV is a reference for client-side JavaScript, covering legacy web browser APIs, the standard Level 2 DOM API, and emerging standards such as the XMLHttpRequest object and the &lt;canvas&gt; tag. ...

    javascript.the.definitive.guide.5th.2006(英文版)

    Part IV is a reference for client-side JavaScript, covering legacy web browser APIs, the standard Level 2 DOM API, and emerging standards such as the XMLHttpRequest object and the &lt;canvas&gt; tag. ...

    Learning React (2nd Edition)

    12 Accessing DOM Elements in React 13 Setting Up Your React Dev Environment 14 Working with External Data in React 15 Building an Awesome Todo List App in React 16 Creating a Sliding Menu in React...

    JavaScript 圣经第5版-Javascript编程宝典--黄金版 .rar

    Chapter 52: Application: Outline-Style Table of Contents. Chapter 53: Application: Calculations and Graphics. Chapter 54: Application: Intelligent "Updated" Flags. Chapter 55: Application: Decision...

    Dart.Essentials.1783989602

    Chapter 4. Developing a Mobile App with Dart Chapter 5. Web Components and polymer.dart Chapter 6. AngularDart Chapter 7. Server-side Applications with Dart Chapter 8. Testing and Profiling the Dart ...

    python3.6.5参考手册 chm

    Python Documentation contents What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted string literals PEP 526: Syntax for variable annotations ...

    Beginning Python (2005).pdf

    Contents Finishing Your Modules 154 Defining Module-Specific Errors 154 Choosing What to Export 155 Documenting Your Modules 156 Try It Out: Viewing Module Documentation 157 Testing Your Module ...

Global site tag (gtag.js) - Google Analytics