×î½ü¿´µ½ÁËÒ»¸öÏîÄ¿½âÎöxmlµÄ·½Ê½£¬¾õµÃÒ²²»´í£¬²âÊÔϸú´ó¼Ò·ÖÏíÏ£¡ÑÛϽâÎöxmlµÄ·½Ê½ºÜ¶à£¬ÕâÀï²»ÌáÁË£¡Ö»Ì¸Ì¸digesterµÄʹÓã¬Ç³Ì¸°É£¡Ï£ÍûÒÔºóÄãÃÇÓöµ½ÁËÄÜÓеã°ïÖú°É£¡
Ê×ÏÈÔÚʹÓÃDigester½âÎöxmlʱҪµ¼Èë2¸öjar°ü£¬ÓеÄÈË˵ÊÇ4¸ö£¬ÎÒÈÏΪ2¸ö¾Í¹»ÁË£¬¿ÉÄÜÓÐÆäËüµÄ¹¦ÄÜʵÏÖ°É£¡²»ËµÁË
2¸öjar°ü·Ö±ðÊÇcommons-beanutils.jar ¡¢commons-digester.jar£¬ÄãÃÇ×Ô¼ºÈ¥ÏÂ!
ÒÔÏÂûÓÐÈκÎÄ¿µÄ£¬´¿Êô²âÊÔÓãº
1,Ê×ÏȽ¨Á¢Ò»¸öwebÏîÄ¿£¨ÎÒÊÇΪÁË·½±ãÖ±½Ó½¨Á¢ÁËwebÏîÄ¿£©½«Èý¸öjar°üµ¼Èë½øÀ´£¬ÔÙ½¨Á¢Ò»¸öxmlÎļþÃû×Ö×Ô¼ºÆð£¬ÎÒÕâÀï½Ðdatabase.xml
<?xml version="1.0" encoding="UTF-8"?>
<database>
<user userName="admin"
password="admin"
>
</user>
</database>
2,Ϊ֮½¨Á¢Ò»¸öjavabean£¬ÊôÐÔ±ØÐëºÍxmlÖеÄuser½ÚµãµÄÊôÐÔÒ»Ö£¨ÊDz»ÊDZØÐëûÓÐ×ö²âÊÔ£©
public class UserBean {
private ......
XML DOM½éÉÜ
Ò»¡¢XML DOM¼ò½é
DOM£¨Document Object Model£¬Îĵµ¶ÔÏóÄ£ÐÍ£©ÊÇÒ»ÖÖÓ¦ÓóÌÐò½Ó¿Ú£¨API£©µÄÓ¦Óã¬Ëü½«Îĵµ£¨ÈçXMLÎĵµ£¬HTMLÎĵµµÈ£©¿´³ÉÊÇÒ»¸öÎĵµ¶ÔÏó£¬È»ºóͨ¹ý³ÌÐòÓïÑÔ£¨ÈçJavaScriptµÈ½Å±¾ÓïÑÔ£¬C++µÈ£©µ÷ÓøÃÎĵµ¶ÔÏ󣬶ÔÎĵµÖеÄÊý¾Ý½øÐдæÈ¡£¬²¢ÀûÓóÌÐò¶Ô»ñÈ¡µÄÊý¾Ý½øÐиú½øÒ»²½µÄ´¦Àí¡£
XML DOMÊǽ«XMLÎĵµÒÔDOM·½Ê½°ü×°£¬Í¨¹ýDOM¼¼Êõ£¬Ó¦ÓóÌÐò¼´¿ÉºÜÈÝÒ×µØÌáÈ¡XMLÎĵµÖеÄÊý¾Ý¡£
¶þ¡¢XML DOM¶ÔÏó
IEʵÏÖÁ˶ÔXML DOM¼¼ÊõµÄÖ§³Ö£¬ÌṩÁËÎå¸ö¿ÉÒÔÔڽű¾³ÌÐòÖе÷ÓõÄXML DOM¶ÔÏó£º
£¨1£© XMLDOMDocument
¸Ã¶ÔÏó´ú±íÕû¸öXMLÎĵµ£¬Ëü¾ßÓжàÖÖÊôÐԺͷ½·¨À´»ñÈ¡»ò´´½¨ÆäËûXML DOM¶ÔÏó¡£
£¨2£© XMLDOMNode
¸Ã¶ÔÏó¿ÉÒÔ´ú±íXMLÎĵµµÄ¸ùÔªËØÒÔ¼°¸ùÔªËØϵĸ÷¸ö½Úµã¡£ËüÖ§³ÖÊý¾ÝÀàÐÍ¡¢ÃûÓò¡¢DTDºÍSchema£¬²¢ÇÒÒÔ´ËÀ©Õ¹ºËÐĵÄXML DOM½Úµã½Ó¿Ú¡£
£¨3£© XMLDOMNodeList
¸Ã¶ÔÏó´ú±íXMLÎĵµÖ ......
XML ÃüÃû¿Õ¼ä¿ÉÌṩ±ÜÃâÔªËØÃüÃû³åÍ»µÄ·½·¨¡£
ÃüÃû³åÍ»
ÓÉÓÚ XML ÖеÄÔªËØÃûÊÇÔ¤¶¨ÒåµÄ£¬µ±Á½¸ö²»Í¬µÄÎĵµÊ¹ÓÃÏàͬµÄÔªËØÃûʱ£¬¾Í»á·¢ÉúÃüÃû³åÍ»¡£
Õâ¸ö XML ÎĵµÐ¯´ø×Åij¸ö±í¸ñÖеÄÐÅÏ¢£º
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
Õâ¸ö XML ÎĵµÐ¯´øÓйØ×À×ÓµÄÐÅÏ¢£¨Ò»¼þ¼Ò¾ß£©£º
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
¼ÙÈçÕâÁ½¸ö XML Îĵµ±»Ò»ÆðʹÓã¬ÓÉÓÚÁ½¸öÎĵµ¶¼°üº¬´øÓв»Í¬ÄÚÈݺͶ¨ÒåµÄ <table> ÔªËØ£¬¾Í»á·¢ÉúÃüÃû³åÍ»¡£
ʹÓÃǰ׺À´±ÜÃâÃüÃû³åÍ»
´ËÎĵµ´øÓÐij¸ö±í¸ñÖеÄÐÅÏ¢£º
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
´Ë XML ÎĵµÐ¯´ø×ÅÓйØÒ»¼þ¼Ò¾ßµÄÐÅÏ¢£º
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length& ......
³£ÓõÄϵÁл¯¶¨Ò壨using System.Xml.Serialization;£©
[XmlAttribute("name")] // ¶¨Òå<Tag name="…"></Tag>
[XmlElement("label")] // ¶¨Òå<label>…</label>
[XmlIgnoreAttribute()] // Ìø¹ýϵÁл¯
[XmlElement("description", IsNullable = false)] // ¶¨Òå<description>…</description>£¬ÔÚÊôÐÔֵΪnullʱ²»ÏÔʾ¸ÃÔªËØ£¬¼´¿ÉÑ¡
[XmlArray("temp_var_list", IsNullable=false)] // ¶¨Òå<temp_var_list><Tag>…</Tag>…<Tag>…</Tag></temp_var_list>£¬¼´Êý×é¶ÔÏóµÄ¸ù½áµã
[XmlArrayItem("temp_var_item")] // ¶¨Òå<temp_var_item>…</temp_var_item>…<temp_var_item>…</temp_var_item>£¬¼´Êý¾ÝÔªË ......
³£ÓõÄϵÁл¯¶¨Ò壨using System.Xml.Serialization;£©
[XmlAttribute("name")] // ¶¨Òå<Tag name="…"></Tag>
[XmlElement("label")] // ¶¨Òå<label>…</label>
[XmlIgnoreAttribute()] // Ìø¹ýϵÁл¯
[XmlElement("description", IsNullable = false)] // ¶¨Òå<description>…</description>£¬ÔÚÊôÐÔֵΪnullʱ²»ÏÔʾ¸ÃÔªËØ£¬¼´¿ÉÑ¡
[XmlArray("temp_var_list", IsNullable=false)] // ¶¨Òå<temp_var_list><Tag>…</Tag>…<Tag>…</Tag></temp_var_list>£¬¼´Êý×é¶ÔÏóµÄ¸ù½áµã
[XmlArrayItem("temp_var_item")] // ¶¨Òå<temp_var_item>…</temp_var_item>…<temp_var_item>…</temp_var_item>£¬¼´Êý¾ÝÔªË ......
ÔÚMFC³ÌÐòÖÐʹÓÃXMLÎļþÅäÖù¤¾ßÀ¸
×÷ÕߣºÖì½ð²Ó
À´Ô´£ºhttp://www.cnblogs.com/clever101
ÏÖÔÚÎÒ·¢ÏÖʹÓÃVisual StudioµÄ×ÊÔ´±à¼Æ÷½øÐбà¼×ÊÔ´ÓÐ×ÅÖî¶àµÄ²»±ã£ºÊ×ÏÈÊÇÈκÎ×ÊÔ´µÄ±ä¶¯Ò»°ã±ä¶¯´úÂ룬²»ÀûÓÚϵͳά»¤£¬Æä´ÎVisual StudioµÄ×ÊÔ´±à¼Æ÷µÄ±¾ÉíµÄ¹¦ÄÜÓÐÏÞ£¬Ò²²»ÀûÓÚ½çÃæÃÀ»¯£¬ÈýÊDz»ÀûÓÚÈËÔ±·Ö¹¤£¬¿ª·¢ÈËÔ±¼ÈҪæʵÏÖ¹¦ÄÜ£¬ÓÖҪæ׼±¸ºÃµÄ½çÃæËزġ£¶Ô½çÃæʵÏÖÎļþÅäÖû¯ÕýÊǽâ¾öÉÏÃæÎÊÌâµÄºÃ·½·¨¡£Õâ´ÎÎÒʵÏÖÁËʹÓÃXMLÎļþÅäÖù¤¾ßÀ¸¡£ÕâÀïËùνÅäÖþÍÊǹ¤¾ßÀ¸µÄ½çÃæÐÅÏ¢È繤¾ßÀ¸±êÌâ¡¢°´Å¥Í¼Æ¬¡¢ÊÇ·ñΪ·Ö¸ô·û¶¼ÔÚXMLÎļþ±£´æ£¬³ÌÐòͨ¹ý½âÎöXMLÎļþÀ´»ñÈ¡¹¤¾ßÀ¸ÐÅÏ¢À´´´½¨¹¤¾ßÀ¸¡£ÕâÑùÒ»µ©·¢ÏÖ½çÃæ²»ºÏÊÊ¿ÉÒÔËæʱÐÞ¸ÄÅäÖÃÎļþ£¬Í¬Ê±ÀûÓÚÈËÔ±·Ö¹¤¡£
¾ßÌåµÄ×ö·¨ÈçÏ£º
1. ÔÚ¹¤³ÌµÄÊä³öĿ¼ÏÂÓÐÒ»¸öSysConfig.xml£¬×÷ΪϵͳÅäÖÃÎļþ¡£ÆäÖйØÓÚ¹¤¾ßÀ¸µÄÅäÖò¿·ÖÈçÏ£º
<AppToolbar valid="1" caption="»ù´¡¹¤¾ß">
<ToolButton file="Add.bmp" />
&nbs ......
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class XMLReader {
private static final String CONFIG_FILE_NAME = "testSuits.xml";
private Element rootElt;
public XMLReader() {
SAXBuilder sb = new SAXBuilder();
try {
//Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE_NAME));
Document doc = sb.build(new FileInputStream("F:/report/testSuits.xml"));
//È¡µÃ<sys-configure>
rootElt = doc.getRootElement(); //Ï൱Äõ½ÁËÕû¸öÎļþÒ»Ñù,¶¼ÔÚrootEltÖÐ
} catch (Exception e) {
e.printStackTrace();
}
}
& ......