xml解析
在java应用开发中我们和xml打交道得机会太平凡了,一般情况下我看会用JDOM或是DOM4j来解析我们得XML文件,下面是一个Dom4j解析xml文件得例子,其中包括了对xml文件得取值、赋值、提取节点、节点得遍历等。
SAXReader reader =
new
SAXReader();
Document doc = reader.read(...);
List childNodes = doc.selectNodes("//Config/Child/ChildNode"
);
for
(Object obj:childNodes) {
Node childNode = (Node)obj;
String name = childNode.valueOf("@name"
);
String text = childNode.getText();
}
一.Document对象相关
1
.读取XML文件,获得document对象.
SAXReader reader = new
SAXReader();
Document document = reader.read(new
File(
"input.xml"
));
2
.解析XML形式的文本,得到document对象.
String text = "<members></members>"
;
Document document = DocumentHelper.parseText(text);
3
.主动创建document对象.
Document document = DocumentHelper.createDocument();
Element root = document.addElement("members"
);
// 创建根节点
二.节点相关
1
.获取文档的根节点.
Element rootElm = document.getRootElement();
2
.取得某节点的单个子节点.
相关文档:
今天的项目模块中用到根据数据库里的资料生成xml文件,主要步骤如下:
(1) 连接数据库,取出数据;
(2) 创建结点,添加子项;
(3) 写入文件“test.xml”中;
具体代码如下:
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Result ......
wsdl.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
  ......
book_schema.xml文件
<?xml version="1.0" encoding="gb2312"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="丛书">
<xs:complexType>
<xs:sequence>
<xs:element name="书">
&n ......
使用Metadata简化表数据向XML形式转化的实现
如果需要将表数据转化为XML形式数据的话,如果我们使用Spring的JDBC Template,那么常需要做的工作是创建一个RowMapper匿名类,在其中将字段与领域对象的某个属性匹配上,然后得到领域对象链表形式的结果,此后展开这个集合,再将字段转化为XML数据,其中进行了两次名称和值之 ......
<?
/**
* xml2array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/xml2array/
* Arguments : $contents - The XML text
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the ......