你这个小孩,真是聪明
可惜,人是要不断进步的
xmlns:xsi是有的,你用google搜索一下就知道。一个XML如果能用IE打开或能够用程序解析就说明语法上是可行的。
以上问题我已经解决了:)
给你代码,自己研究一下吧
package com.tjsoft.test;
import java.io.*;
//import org.jdom.*;
//import org.jdom.input.*;
//import org.jdom.output.*;
import org.dom4j.DocumentHelper;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;
import java.util.List;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.io.OutputFormat;
/**
* <p>Title: Example for Ciss</p>
* <p>Description: Work for Study.......</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: c</p>
* @author
* @version 1.0
*/
public class CMakeXML
{
public CMakeXML()
{
}
/*
public void BuildXMLDoc() throws IOException, JDOMException
{
Element eRoot, e1, e2;
Document doc;
eRoot = new Element(\"Message\");
doc = new Document(eRoot);
eRoot = doc.getRootElement();
eRoot.setAttribute(\"Version\", \"1.0\");
eRoot.setAttribute(\"Xmlns\", \"http://www.egxml.gov.cn/message\");
eRoot.setAttribute(\"Smlnsxsi\", \"http://www.w3.org/2001/XMLSchema-instance\");
eRoot.setAttribute(\"XsischemaLocation\", \"http://www.egxml.gov.cn/message message.xsd\");
e1 = new Element(\"egXML\");
e2 = e1.setText(\"aaa\");
e1 = eRoot.addContent(e2);
e1.setAttribute(\"Version\", \"1.9\");
e1 = new Element(\"display\");
e2 = e1.setText(\"bbb\");
e1 = eRoot.addContent(e2);
e1 = new Element(\"eSign\");
e2 = e1.setText(\"测试\");
e1 = eRoot.addContent(e2);
e1 = new Element(\"eSealContainer\");
e2 = e1.setText(\"ddd\");
e1 = eRoot.addContent(e2);
//Format format = Format.getCompactFormat();
Format format = Format.getPrettyFormat();
//Format format = Format.getRawFormat();
format.setEncoding(\"gb2312\");
XMLOutputter XMLOut = new XMLOutputter();
XMLOut.setFormat(format);
XMLOut.output(doc, new FileOutputStream(\"test1.xml\"));
}*/
/**
* 建立一个XML文档,文档名由输入参数决定
* @param filename 需建立的文件名
* @return 返回操作结果, 0表失败, 1表成功
*/
public int createXMLFile(String filename)
{
/** 返回操作结果, 0表失败, 1表成功 */
int returnValue = 0;
/** 建立document对象 */
Document document = DocumentHelper.createDocument();
/** 建立XML文档的根books */
Element booksElement = document.addElement(\"books\");
//booksElement = document.getRootElement();
booksElement.addAttribute(\"Version\", \"1.0\");
booksElement.addAttribute(\"xmlns\", \"http://www.egxml.gov.cn/message\");
booksElement.addAttribute(\"xmlns:xsi\", \"http://www.w3.org/2001/XMLSchema-instance\");
booksElement.addAttribute(\"xsi:schemaLocation\", \"http://www.egxml.gov.cn/message message.xsd\");
/** 加入一行注释 */
booksElement.addComment(\"This is a test for dom4j, holen, 2004.9.11\");
/** 加入第一个book节点 */
Element bookElement = booksElement.addElement(\"book\");
/** 加入show参数内容 */
bookElement.addAttribute(\"show\", \"yes\");
/** 加入title节点 */
Element titleElement = bookElement.addElement(\"title\");
/** 为title设置内容 */
titleElement.setText(\"Dom4j Tutorials\");
/** 类似的完成后两个book */
bookElement = booksElement.addElement(\"book\");
bookElement.addAttribute(\"show\", \"yes\");
titleElement = bookElement.addElement(\"title\");
titleElement.setText(\"Lucene Studing\");
bookElement = booksElement.addElement(\"book\");
bookElement.addAttribute(\"show\", \"no\");
titleElement = bookElement.addElement(\"title\");
titleElement.setText(\"Lucene in Action\");
/** 加入owner节点 */
Element ownerElement = booksElement.addElement(\"owner\");
ownerElement.setText(\"O\'Reilly\");
try
{
/** 将document中的内容写入文件中 */
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(\"GB2312\"); //中文
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)), format);
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
returnValue = 1;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return returnValue;
}
/**
* 修改XML文件中内容,并另存为一个新文件
* 重点掌握dom4j中如何添加节点,修改节点,删除节点
* @param filename 修改对象文件
* @param newfilename 修改后另存为该文件
* @return 返回操作结果, 0表失败, 1表成功
*/
public int ModiXMLFile(String filename, String newfilename)
{
int returnValue = 0;
try
{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filename));
/** 修改内容之一: 如果book节点中show参数的内容为yes,则修改成no */
/** 先用xpath查找对象 */
List list = document.selectNodes(\"/books/book/@show\");
Iterator iter = list.iterator();
while (iter.hasNext())
{
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals(\"yes\"))
{
attribute.setValue(\"no\");
}
}
/**
* 修改内容之二: 把owner项内容改为Tshinghua
* 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个参数type
*/
list = document.selectNodes(\"/books/owner\");
iter = list.iterator();
if (iter.hasNext())
{
Element ownerElement = (Element) iter.next();
ownerElement.setText(\"Tshinghua\");
Element dateElement = ownerElement.addElement(\"date\");
dateElement.setText(\"2004-09-11\");
dateElement.addAttribute(\"type\", \"Gregorian calendar\");
}
/** 修改内容之三: 若title内容为Dom4j Tutorials,则删除该节点 */
list = document.selectNodes(\"/books/book\");
iter = list.iterator();
while (iter.hasNext())
{
Element bookElement = (Element) iter.next();
Iterator iterator = bookElement.elementIterator(\"title\");
while (iterator.hasNext())
{
Element titleElement = (Element) iterator.next();
if (titleElement.getText().equals(\"Dom4j Tutorials\"))
{
bookElement.remove(titleElement);
}
}
}
try
{
/** 将document中的内容写入文件中 */
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(\"GB2312\"); //中文
XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename)), format);
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
returnValue = 1;
}
catch (Exception e)
{
System.out.println(\"出错1:\"+e.getLocalizedMessage());
e.printStackTrace();
}
}
catch (Exception ex)
{
System.out.println(\"出错2:\"+ex.getLocalizedMessage());
ex.printStackTrace();
}
return returnValue;
}
//
// /**
// * 格式化XML文档,并解决中文问题
//
// * @param filename
//
// * @return
//
// */
//
// public int formatXMLFile(String filename)
// {
// int returnValue = 0;
// try
// {
// SAXReader saxReader = new SAXReader();
// Document document = saxReader.read(new File(filename));
// XMLWriter output = null;
// /** 格式化输出,类型IE浏览一样 */
//
// OutputFormat format = OutputFormat.createPrettyPrint();
// /** 指定XML字符集编码 */
//
// format.setEncoding(\"GBK\");
// output = new XMLWriter(new FileWriter(new File(filename)), format);
// output.write(document);
// output.close();
// /** 执行成功,需返回1 */
//
// returnValue = 1;
// }
// catch (Exception ex)
// {
// ex.printStackTrace();
// }
// return returnValue;
// }
public static void main(String[] args)
{
/*
try
{
CXmlMaker s1 = new CXmlMaker();
System.out.println(\"Now we build an XML document .....\");
s1.BuildXMLDoc();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(\"Now we build an XML document .....\");
*/
CMakeXML temp = new CMakeXML();
System.out.println(temp.createXMLFile(\"d://holen.xml\"));
System.out.println(temp.ModiXMLFile(\"d://holen.xml\", \"d://holen2.xml\"));
//System.out.println(temp.formatXMLFile(\"d://holen.xml\"));
}
}