XML的读写
XML是一种可扩展置标语言,又称可扩展的编辑语言。XML文档的定义格式有两种:DTD和Schema格式,由于Schema是xml本身的,所以应用的非常普遍。xml的作用是文件的读写,所以在web开发中也得到了广泛应用,作为一种配置文件,充分发挥了它读写的功能。XML的解析方式有四种:DOM,SAX,JDOM,DOM4J。DOM是一种标准模型,也是W3C所推荐的。几种解析方式各有优缺点,但是DOM4J几种了前几种的有点,在web开发中得到广泛应用,本人也推荐掌握DOM4J。四种解析方式至少掌握一种即可,如果自己有时间精力最好全部掌握。下面是以DOM4J进行文件的读写实例:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4j {
public static void main(String[] args) {
File file=new File("src/XML/gg.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
write(file);
read(file);
}
//XML的写入
private static void write(File file) {
// 创建document文档
Document doc=DocumentHelper.createDocument();//得到document文件
//添加元素
Element e=doc.addElement("root");//添加根元素
Element student=e.addElement("student");//添加子元素
Element name=student.addElement("name");
Element age=student.addElement("age");
Element num=student.addElement("num");
Element hight=student.addElement("hight");
//给元素赋值
student.a
相关文档:
<?
/**
* 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 ......
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/bea ......
在项目中,同一个配置在不同的目录下要有不同的值,而目录又是不确定的,这时就需要将配置信息存放在相应的目录中,在运行时根据路径去取
方法:用xml文件存储,放在使用目录下,用下面方法获取配置信息
public class yzzConfig
{
/// <summary>
/// 获取Xml文件配置信息
/// ......
XmlDocument XMLFile = new XmlDocument();
XMLFile.Load(HttpContext.Cur ......
使用SAXReader需要导入dom4j-full.jar包。
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。
&n ......