您的位置:首页 > 编程语言 > Java开发

JDOM的Element的getChild(String name)和getChildren((String name)方法

2017-04-21 13:48 405 查看
Element org.jdom.Element.getChild(String name)

Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.

例子:

这是一个XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 定义了导出的excel的导出文件            XML的根节点          -->
<excel id="student" code="student" name="学生信息导入">
<!-- 定义了excel的几列多少宽度 -->
<colgroup>
<col index="A" width='17em'></col>
<col index="B" width='17em'></col>
<col index="C" width='17em'></col>
<col index="D" width='17em'></col>
<col index="E" width='17em'></col>
<col index="F" width='17em'></col>
</colgroup>
<!-- 定义了excel的第一行的高度和合并6个单元格值为信息信息导入 -->
<title>
<tr height="16px">
<td rowspan="1" colspan="6" value="学生信息导入" />
</tr>
</title>
<!-- 定义了excel的表头列名和高度 -->
<thead>
<tr height="16px">
<th value="编号" />
<th value="姓名" />
<th value="年龄" />
<th value="性别" />
<th value="出生日期" />
<th value=" 爱好" />
</tr>
</thead>
<!-- 定义了excel的数据区的样式 -->
<tbody>
<tr height="16px" firstrow="2" firstcol="0" repeat="5">
<td type="string" isnullable="false" maxlength="30" /><!--用户编号 -->
<td type="string" isnullable="false" maxlength="50" /><!--姓名 -->
<td type="numeric" format="##0" isnullable="false" /><!--年龄 -->
<td type="enum" format="男,女" isnullable="true" /><!--性别 -->
<td type="date" isnullable="false" maxlength="30" /><!--出生日期 -->
<td type="enum" format="足球,篮球,乒乓球" isnullable="true" /><!--爱好 -->
</tr>
</tbody>
</excel>


//获取需要进行解析的xml文件路径: F:\Web\excel_drdc       /src/student.xml
String path = System.getProperty("user.dir") + "/src/student.xml";
File file = new File(path);
//JDOM 它自身不包含解析器。它通常使用 SAX2 解析器来解析和验证输入 XML 文档
//SAXBuilder是一个JDOM解析器 能将路径中的XML文件解析为Document对象
SAXBuilder builder = new SAXBuilder();//创建一个新的SAXBuilder-JDOM解析器来解析XML文件
//解析xml文件:使用SAXBuilder的实例化对象builder的build方法,将路径中的XML文件解析为Document对象
Document parse = builder.build(file);
//获取xml文件根节点    <excel id="student" code="student" name="学生信息导入">
Element root
b7be
= parse.getRootElement();
//获取模板名称,也就是获取根节点的name属性   root.getAttribute("name").getValue()
String templateName = root.getAttribute("name").getValue();//templateName   =   "学生信息导入";


参考链接JDOM 操作XML

* getChildren方法可以获取所有子元素

* getChildren(elName)可以获取所有名称为elName的子节点

* getAttributeValue可以获取指定属性的值

* getChildText可以获取子节点的文本值

getChild(String name)方法

Element colgroup = root.getChild("colgroup");//获取根节点<excel>的子节点<colgroup>
Element title = root.getChild("title");//取得根节点<excel>的子节点<title>


getChildren((String name)方法

List<Element> trs = title.getChildren("tr");//取得<title>节点的所有<tr>标签
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐