python´¦ÀíXML
»ù±¾É϶¼ÊÇʹÓÃpythonÀ´½âÎöxmlÎļþµÄ¡£
±ÈÈçÎÒÒª½«ÄÚÈÝΪ
<?xml version="1.0" encoding="utf-8"?>
<root>
<book isbn="34909023">
<author>
dikatour
</author>
</book>
</root>
дÈëµ½xmlstuff.xmlÎļþÖÐÈ¥¡£
ÆäʵҲºÜ¼òµ¥£¬»ù±¾ÔÀíÈçÏ£º
ÎÒʹÓÃxmlµÄDOM·½Ê½£¬ÏÈÔÚÄÚ´æÖд´½¨Ò»¸ö¿ÕµÄDOMÊ÷£¬È»ºó²»¶ÏÔö¼ÓÎÒÒªµÄ½Úµã£¬×îºóÐγÉÎÒÏëÒªµÄDOM£¬×îºóÊä³öµ½ÎļþÖÐÈ¥¡£
1.ÎÒʹÓÃxml.dom.minidomÕâ¸ömoduleÀ´´´½¨xmlÎļþ
from xml.dom import minidom
2. ÿ¸öxmlÎļþ¶¼ÊÇÒ»¸öDocument¶ÔÏó,´ú±í×ÅÄÚ´æÖеÄDOMÊ÷
doc = minidom.Document()
3.ÓÐÁË¿ÕµÄDOMÊ÷ºó£¬ÎÒÃÇÔÚÉÏÃæÌí¼Ó¸ù½Úµã
rootNode = doc.createElement("root")
doc.appendChild(rootNode) #×¢ÒâpythonµÄlibrary referenceÀï˵,createElementºó²¢Ã»Óн«½Úµã¶ÔÏó¼Óµ½DOMÊ÷ÉÏ£¬ÐèÒª×Ô¼ºÊÖ¹¤¼ÓÉÏ
4.´´½¨ÆäËüµÄ½Úµã
5.Êä³öµ½xmlÎļþÖÐÈ¥
doc.writexml(f, "\t", "\t", "\n", "utf-8") #µÚÒ»¸ö²ÎÊýf¾ÍÊÇÄãµÄÄ¿±êÎļþ¶ÔÏ󣬵ڶþ¸ö²ÎÊýºÃÏñÊÇ<?xml>ºÍÏÂÃæÒ»¸ö¸ù½ÚµãµÄËõ½øÅÅÁиñʽ,
µÚÈý¸ö²ÎÊýºÃÏñÊÇÆäËû½ÚµãÓë×Ó½ÚµãµÄËõ½øÅÅÁиñʽ£¬µÚËĸö²ÎÊýÖƶ¨ÁË»»Ðеĸñʽ(Èç¹ûÄãÌîÈë" "£¬ÄǾͲ»»»ÐÐÁË£¬ËùÓеÄxml¶¼ËõÔÚÁËÒ»ÐÐÉÏÃæ :) )
£¬µÚÎå¸ö²ÎÊýÖƶ¨ÁËxmlÄÚÈݵıàÂë¡£³ýÁ˵ÚÒ»¸ö²ÎÊýÊDZØÐëµÄ£¬ÆäËû²ÎÊý¶¼ÊÇ¿ÉÑ¡ÔñµÄ¡£
×îÖÕ´úÂëÈçÏÂ(Õâ¸ö³ÌÐòûʲô¼ÛÖµ£¬Ö»ÊÇÓÃÀ´²âÊÔÑéÖ¤×Ô¼ºµÄÏë·¨,Äã¸ü¿ÉÄܶ¨ÒåÒ»¸ö¼òµ¥Àà»òº¯Êý£¬½«ÄãµÄÊý¾Ý½á¹¹ÐòÁл¯µ½xmlÎļþÖÐ)£º
from xml.dom import minidom
import traceback
try:
f = open("xmlstuff.xml", "w")
try:
doc = minidom.Document()
rootNode = doc.createElement("root")
Ïà¹ØÎĵµ£º
ÕÛÌÚ¼ÆËã»ú£¬±»¼ÆËã»úÕÛÌÚ£¬Ç°Ç°ºóºóÒ²ÓÐÊ®¼¸Äê¡£
×î¿É±¯µÄÊǾÓȻһÎÞËù³É£¬ËäÈ»½ö½ö³öÓÚÐËȤ£¬²¢ÎÞ¹¤×÷֮ѹÁ¦£¬µ«ÊÇÕâÖÖ´ìÕÛ¸ÐȷʵÊÇʵ´òʵµÄ¡£
ÒѾ¼Ç²»ÆðÊÇÄÄÒ»ÌìÁË£¬ºÃÏñͻȻ¾ÍÖªµÀÁËpythonµÄ´æÔÚ£¬´Ó×î³õµÄ²»¾Ò⣬µ½Ô½À´Ô½Ï²°®£¬ÎÒÒ»²½²½µÄÏòpython¿¿½ü¡£
ÎÒϲ»¶Python£¬ÕâÊÇÒ»¸öÊÂʵ£¬ÎÒ׫´ ......
С°×¶ÔÉϽڿÎÀïµÄ“СÂéȸ”ÐËȤ°»È»£¬ÕûÌì¾ÍÔÚËÞÉáµ·¹Ä¡£Õâ²»£¬¼¸ÌìÏÂÀ´ËûÒѾ¹¥ÏÂÊ®¼¸¸öÍøÕ¾ÁË¡£
С²Ëȴϲ»¶Ò»¸öÈË×øÔÚµçÄÔÅԱߣ¬Öå×Åüͷ£¬ÐÀÉÍ×ÅÕâÒ»ÐÐÐÐ×Ô¼º¿´²»¶®µÄ´úÂë¡£“ÕâЩ×Ö·ûºÃ¹ÖÒìѽ£¡”С²ËÍá×ÅÍ·£¬“ËüΪɶ½ÐÕýÔò±í´ïʽÄØ£¿”
°Ù¶Èһϣ¬ÖÕÓÚÖªµÀ¡£Ð¡² ......
ÕýÔò±í´ïʽÊǸöħ¹í£¬Ò²ÊǸöÌìʹ¡£ÔÚÄãûÓÐÕÆÎÕËü֮ǰ£¬ËüÊÇħ¹í£¬ÔÚÄãÕÆÎÕËüºó£¬ËüÊÇÌìʹ£¬µ«ÊÇ£¬Ê±
²»Ê±»¹ÊÇÒªÌø³öÀ´£¬¸øÄãµ÷Ƥµ·µ°Ò»·¬¡£
Ò»¸öÕýÔò±í´ïʽ¾ÍÊÇÓÉÆÕͨ×Ö·ûÒÔ¼°ÌØÊâ×Ö·û×é³ÉµÄÎÄ×Öģʽ£¬¸ÃģʽÃèÊöÔÚ²éÕÒÎÄ×ÖÖ÷Ìåʱ´ý
Æ¥ÅäµÄÒ»¸ö»ò¶à¸ö×Ö·û´®¡£ÕýÔò±í´ïʽ×÷Ϊһ¸öÄ£°å£¬½«Ä³¸ö×Ö·ûģʽÓëËùËÑË÷µÄ ......
python¶Ô¶à¹úÓïÑԵĴ¦ÀíÊÇÖ§³ÖµÄºÜºÃµÄ£¬Ëü¿ÉÒÔ´¦ÀíÏÖÔÚÈÎÒâ±àÂëµÄ×Ö·û£¬ÕâÀïÉîÈëµÄÑо¿Ò»ÏÂpython¶Ô¶àÖÖ²»Í¬ÓïÑԵĴ¦Àí¡£
ÓÐÒ»µãÐèÒªÇå³þµÄÊÇ£¬µ±pythonÒª×ö±àÂëת»»µÄʱºò£¬»á½èÖúÓÚÄÚ²¿µÄ±àÂ룬ת»»¹ý³ÌÊÇÕâÑùµÄ£º
ÔÓбàÂë -> ÄÚ²¿±àÂë ->
Ä¿ ......