DOM4JÉú³ÉXMLÎĵµ
public int createXMLFile(String filename) {
int returnValue = 0;
Document document = DocumentHelper.createDocument(); //Éú³ÉDocument£¬ÓÃÓÚ¹ÜÀíXMLÎĵµ
Element booksElement = document.addElement("books"); //Ìí¼Ó¸ù½Úµã
booksElement.addComment("This is a test for dom4j, holen, 2004.9.11"); //Ìí¼Ó×¢ÊÍ
Element bookElement = booksElement.addElement("book"); //ÔÚ¸ú½ÚµãbooksÏÂÌí¼ÓÒ»¸öbook×Ó½Úµã
bookElement.addAttribute("show", "yes"); //ΪbookElementÌí¼ÓÊôÐÔ£¬²¢Öƶ¨ÊôÐÔµÄÖµ
Element titleElement = bookElement.addElement("title"); //ΪbookElement½ÚµãÌí¼ÓÒ»¸ötitle×Ó½Úµã
titleElement.setText("Dom4j Tutorials"); //Ϊtitle×Ó½ÚµãÉèÖÃÎı¾½ÚµãÄÚÈÝ
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document); //ÆÕͨ¸ñʽ£¬±È½Ï²»ÃÀ¹Û
OutputFormat format =null;
// format=OutputFormat.createPrettyPrint(); //ÃÀ»¯¸ñʽ,¶ÁÈ¡ºóÃÀ»¯ÏÔʾ
// writer = new XMLWriter( System.out, format );
// writer.write( document );
// format = OutputFormat.createCompactFormat(); //Ëõ¼õ¸ñʽ£¬¶ÁÈ¡ºóËõ¼õÏÔʾ
// writer = new XMLWriter( System.out, format );
// writer.write( document );
writer.close();
return
Ïà¹ØÎĵµ£º
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item ASIN="0446355453"
Author="Sidney Sheldon"
Manufacturer="Warner Books"
ProductGroup="Book"
Title="Master of the Game"/>
<Item ASIN=" ......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- ÅäÖúÍÓÅ»¯SQL Map½âÎöʵÀýµÄÉ趨 -->
<settings cacheModelsEnabled=" ......
declare @xml xml
set @xml = '<root/>'
select @xml
declare @value varchar(10)
set @value = 'val1'
set @xml.modify('insert <item value="{sql:variable("@value")}" /> into (/root)[1]')
select @xml
set @value = 'val2'
set @xml.modify('replace value of (/root/item/@value)[1] with "val2 ......
Java codeFormat ft=Format.getRawFormat();
ft.setEncoding("utf-8");
ft.setIndent(" ");//ÉèÖÃËõ½ø
XMLOutputter outputter=new XMLOutputter(ft);//ÉèÖÃXML¸ñʽ
Õâλ˵µÄ²»´í£»
ÔÚieÀïÃæ£¬ËüÄܰÑxml×Ô¶¯½âÎö³ÆÊ÷Ðνṹ£¬ËùÒÔ¿´ÆðÀ´Ã»ÎÊÌ⣬ÔÚ¼Çʱ¾ÀïÃæ£¬¾Í²»ÐÐÁ ......