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
.取得某节点的单个子节点.
相关文档:
/*
练习使用java.util.properties类包来操作propertes及XML文件,通过store方法的调用可实现xml/properties文件的相互保存转化
*/
import java.util.*;
import java.io.*;
public class TestPropertes
{
public static void main(String[] args) {
Properties pp = new Properties();
Fi ......
今天的项目模块中用到根据数据库里的资料生成xml文件,主要步骤如下:
(1) 连接数据库,取出数据;
(2) 创建结点,添加子项;
(3) 写入文件“test.xml”中;
具体代码如下:
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Result ......
个人收集、整理了一些LINQ TO XML的基本方法,希望各位大虾多多指导:
/// <summary>
///Xml节点属性
/// </summary>
public class XmlAttribute
{
public XmlAttribute()
{
}
public XmlAttribute(string _key,object _value)
&nbs ......
package com.pk.xml;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4j {
public static void main(String[] args) {
try {
//获得SAX解析器
SAXReader reader = new SAXReader();
//解析文件
File file = n ......
<?
/**
* 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 ......