DOM4J:
==================================================================================
1、写XML
public void save() throws Exception {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
OutputStream fos = new FileOutputStream(xmlFileName);
XMLWriter writer = new XMLWriter(fos, format);
writer.write(xmlDoc);
writer.close();
}
2、读取XML
InputStream is = new FileInputStream(config);//config:xml文件所在的路径
SAXReader sax=new SAXReader();
Document doc=sax.read(is);
List sourceList = doc.selectNodes("/data-source/source");//xml二级元素
Iterator iterator = sourceList.iterator();
while(iterator.hasNext()){
Element source = (Element)iterator.next();
DataSourceBean dsBean = new DataSourceBean();
dsBean.setName(source.elementText("name"));
// System.out.println(source.elementText("name"));
dsBean.setUrl(source.elementText("url"));
}
==================================================================================
JDOM:
==================================================================================
1、写XML:
Format format = Format.getCompactFormat();
format.setEncoding("utf-8"); //设置xml文件的字符为utf-8
format.setIndent(" "); //设置xml文件的缩进为4个空格
XMLOutputter XMLOut = new XMLOutputter(format);
/*
* 将doc1中的各个节点及值放入文件fileName2中。
*/
XMLOut.output(doc1, new FileOutputStream(fileName2));
2、形成DOC:
Element root1=new Element("video");
Document doc1=new Document(root1);//将根元素植入文档doc中
root1.setAttribute("allnum",String.valueOf(i));
遍历一个集合创建N个record节点
Element record =new Element("record");
Element id=new Element("id");
id.setText(modifyString(m.getId()));
record.addContent(id);
。。。
Element name=new Element("name");
name.setText(modifyString(m.getName()));
record.addContent(name);
root1.addContent(record);
3、读取XML:
SAXBuilder saxBuilder = new SAXBuilder();
org.jdom.Document doc = saxBuilder.build(url);
Element root = doc.getRootElement();
List allChild = root.getChildren();
Element el = (Element)allChild.get(i);
Element el1 =(Element) el.getChild("keyword");
Element el2 =(Element) el.getChild("path");
String keyword = el1.getText();
String path = el2.getText();
==================================================================================
自制作:
==================================================================================
1、写XML:
public static void writeXml(ImageStore[] ir, int allnum, PrintWriter writer)throws IOException
{
writer.write("\n");
if (ir == null || ir.length == 0)
{
writer.write("no result");
return;
}
writer.write("\n");
for (int i = 0; i < ir.length; i++)
{
System.out.println(ir.length+" "+i);
// if (ir[i] == null)
// continue;
writer.write("\n");
writer.write("");
writer.write(ir[i].getKeyword());
writer.write("\n");
writer.write("");
writer.write(ir[i].getCurrank().toString());
writer.write("\n");
writer.write("\n");
}
writer.write("\n");
}
2、读取XML:
public void parse(String content) {
int start, end;
start = content.indexOf("allnum=");
if (start == -1) {
allnum = 0;
}else {
allnum = Integer.parseInt(content.substring(start + 8, content.indexOf("'", start + 8)));
}
//首先根据头文件确定循环次数
String[] records = parseTag(content, _RECORD);
resultList = new ArrayList();
for (int i = 0; i < records.length; i++) {
//解析出来的内容放到一个数据bean里面去
SingleGameResult sgr = new SingleGameResult();
sgr.setShowOrder(i + 1);
String[] items;
items = parseTag(records[i], _TITLE);
if ((items != null) && (items.length > 0)) {
sgr.setTitle(items[0]);
}
items = parseTag(records[i], _URL);
if ((items != null) && (items.length > 0)) {
sgr.setUrl(items[0]);
}
items = parseTag(records[i], _FORMAT);
if ((items != null) && (items.length > 0)) {
sgr.setFormat(items[0]);
}
items = parseTag(records[i], _SIZE);
。。。。。
resultList.add(sgr);
}
}
--------------------------------------
public static String[] parseTag(String content, String tag)
{
final String _TAG_START = "<" + tag + ">";
final String _TAG_END = "" + tag + ">";
ArrayList results = new ArrayList();
int start = content.indexOf(_TAG_START);
int end = 0;
while (start >= 0) {
end = content.indexOf(_TAG_END, start);
if (end < start) {
break;
}
String item = content.substring(start + _TAG_START.length(), end);
start = content.indexOf(_TAG_START, end);
results.add(item);
}
return (String[]) results.toArray(new String[0]);
}
==================================================================================