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文档的节点有点乱,不是树型的。
相关文档:
大量SmipleXML函数可用来加载和解析大量XML文档。
1.simpleXML_load_file()函数来加载指定的XML文件到对象。如果加载文件时遇到问题,则返回FLASE。例:
book.xml文件:
<?xml version="1.0" standalone="yes"?>
<library>
<book>
<title>Pride and Prejudice</title>
< ......
在java应用开发中我们和xml打交道得机会太平凡了,一般情况下我看会用JDOM或是DOM4j来解析我们得XML文件,下面是一个Dom4j解析xml文件得例子,其中包括了对xml文件得取值、赋值、提取节点、节点得遍历等。
SAXReader reader =
new
SAXReader();
Document doc = reader.read(...); &nb ......
刚开始学习语义网的知识,根据语义网层次推进,看完XML紧接着RDF,忽然有个疑问:为什么我们一定要用RDF而非仅仅使用已经很成熟的XML呢?貌似XML比RDF少了一个推理的系统,可以就推理而言,RDF也绝不是最好的,显然OWL的推理要比RDF强很多啊,那么为什么还需要这个层次呢?
&n ......
1. 一般情况下使用以下代码即可将XML字符串重新格式化:
private string FormatXml(string source)
{
StringBuilder ......