JDOM生成XML文档
首先下载JDOM.JAR加入的classpath中
package com.test.search;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
public class ToXML {
public void BuildXMLDoc(List<SearchBean> list) throws IOException, JDOMException {
//创建跟节点
Element root=new Element("search");
//将跟节点添加到文档中
Document document=new Document(root);
for (int i = 0; i < list.size(); i++) {
SearchBean bean=list.get(i);
Element element=new Element("object");
element.addContent(new Element("URL").setText(bean.getUrl()));
element.addContent(new Element("TITLE").setText(bean.getTitle()));
element.addContent(new Element("DESCRIBE").setText(bean.getDiscribe()));
element.addContent(new Element("INFO").setText(bean.getInfo()));
root.addContent(element);
}
XMLOutputter out=new XMLOutputter();
out.output(document, new FileOutputStream("search.xml"));
}
/*public static void main(String[] args) {
ToXML toXML=new ToXML();
}*/
}
生成完成,只是生成的xml文档的节点有点乱,不是树型的。
相关文档:
xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
<wap2>false</wa ......
<?
/**
* 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 ......
以下是一个通过minidom模块写文件的完整示例,是在最近做的项目Walle上面用到的,这个示例的目的是生成一个如下的格式的xml,文件格式为无BOM utf-8。
生成xml文件格式:
<?xml version="1.0" encoding="utf-8"?>
<coverages>
<coverage>
  ......
<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文件配置信息
/// ......