Java²Ù×÷XMLÎļþ dom4j ƪ¡¾×ª¡¿
ÔÚÏîÄ¿ÖУ¬ÎÒÃǺܶ඼Óõ½ÁËxmlÎļþ£¬ÎÞÂÛÊDzÎÊýÅäÖû¹ÊÇÓëÆäËüϵͳµÄÊý¾Ý½»»¥¡£
½ñÌì¾ÍÀ´½²Ò»ÏÂJava ÖÐʹÓÃdom4jÀ´²Ù×÷XMLÎļþ¡£
ÎÒÃÇÐèÒªÒýÈëµÄ°ü£º
//Îļþ°ü
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
//¹¤¾ß°ü
import java.util.Iterator;
import java.util.List;
//dom4j°ü
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
1¡¢½«XMLÎļþµÄÄÚÈÝת»¯ÎªString
/**
* doc2String
* ½«xmlÎĵµÄÚÈÝתΪString
* @return ×Ö·û´®
* @param document
*/
public static String doc2String(Document document)
{
String s = "";
try
{
//ʹÓÃÊä³öÁ÷À´½øÐÐת»¯
ByteArrayOutputStream out = new ByteArrayOutputStream();
//ʹÓÃGB2312±àÂë
OutputFormat format = new OutputFormat(" ", true, "GB2312");
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
s = out.toString("GB2312");
&n
Ïà¹ØÎĵµ£º
ÔÚjavaÓïÖÐGC
¼´À¬»øÊÕ¼¯»úÖÆÊÇÖ¸jvmÓÃÓÚÊÍ·ÅÄÇЩ²»ÔÙʹÓõĶÔÏóËùÕ¼ÓõÄÄÚ´æ¡£javaÓïÑÔ²¢²»ÒªÇójvmÓÐgc£¬Ò²Ã»Óй涨gcÈçºÎ¹¤×÷¡£²»¹ý³£ÓõÄjvm¶¼ÓÐgc£¬¶øÇÒ´ó¶àÊýgc¶¼Ê¹ÓÃÀàËƵÄËã·¨¹ÜÀíÄÚ´æºÍÖ´ÐÐÊÕ¼¯²Ù×÷¡£
À¬»øÊÕ¼¯µÄÄ¿µÄÔÚÓÚÇå³ý²»ÔÙʹÓõĶÔÏó¡£gcͨ¹ýÈ·¶¨¶ÔÏóÊÇ·ñ±»»î¶¯¶ ......
ÔÚ¹¤×÷Öо³£Óöµ½µ½ÁíÍâÒ»¸ö·þÎñÆ÷¶ÁÈ¡Êý¾Ý£¬ÆäÖÐÒ»ÖÖ×î³£Óõķ½·¨£¬¾ÍÊÇÓÃxml½øÐÐÊý¾Ý´«µÝ£¬±ÈÈçA·þÎñÆ÷µ½B·þÎñÆ÷µÃµ½¶ÔÓ¦µÄxml¸ñʽµÄÊý¾Ý£¬AµÃµ½Ö®ºó½øÐнâÎö¡£Ò»°ãÕâÖÖ½âÎö·ÖΪDOM½âÎöºÍSAX½âÎö¡£ÕâÀï²»ÊÇ̽ÌÖDOMÓëSAX½âÎöµÄÇø±ðºÍÓŵ㣬ֻÊǽéÉÜÒ»¸öС¿ò¼Ü£¬ÊµÏÖJAVABeanºÍXMLµÄת»»£¬Éµ¹ÏʽµÄת»»¡£----- ......
ÔÚ×öÒ»°ãµÄXMLÊý¾Ý½»»»¹ý³ÌÖУ¬ÎÒ¸üÀÖÒâ´«µÝXML×Ö·û´®£¬¶ø²»ÊǸñʽ»¯µÄXML Document¡£Õâ¾ÍÉæ¼°µ½XML×Ö·û´®ºÍXml DocumentµÄת»»ÎÊÌ⣬˵°×ÁËÕâÊǸöºÜ¼òµ¥µÄÎÊÌ⣬±¾Îľ͸÷ÖÖXML½âÎöÆ÷·Ö±ðÁоÙÈçÏ£¬ÒÔ·½±ã×Ô¼º½ñºó²éÔÄ¡£
Ò»¡¢Ê¹ÓÃ×îÔʼµÄjavax.xml.parsers£¬±ê×¼µÄjdk api
// ×Ö·û´®×ªXML
String xmlStr = "......";
S ......
XMLÊÇÒ»ÖÖ¿ÉÀ©Õ¹ÖñêÓïÑÔ£¬ÓֳƿÉÀ©Õ¹µÄ±à¼ÓïÑÔ¡£XMLÎĵµµÄ¶¨Òå¸ñʽÓÐÁ½ÖÖ£ºDTDºÍSchema¸ñʽ£¬ÓÉÓÚSchemaÊÇxml±¾ÉíµÄ£¬ËùÒÔÓ¦Óõķdz£Æձ顣xmlµÄ×÷ÓÃÊÇÎļþµÄ¶Áд£¬ËùÒÔÔÚweb¿ª·¢ÖÐÒ²µÃµ½Á˹㷺ӦÓã¬×÷ΪһÖÖÅäÖÃÎļþ£¬³ä·Ö·¢»ÓÁËËü¶ÁдµÄ¹¦ÄÜ¡£XMLµÄ½âÎö·½Ê½ÓÐËÄÖÖ£ºDOM£¬SAX£¬JDOM£¬DOM4J¡£ ......