我通过minidom.parseString把一段string转化成xml格式进行解析,现在解析出来进行修改之后,想把xml文件转化成string要怎么做?
ä½ è¯´çä¸æ¯xmlæ件å§ï¼æ¯xml对象转åæstringå§ã
ä½ å¯ä»¥ä½¿ç¨toxml()è¿ä¸ªæ¹æ³ã
Node.toxml([encoding])å¦æ解å³äºæ¨çé®é¢è¯·é纳ï¼
å¦ææªè§£å³è¯·ç»§ç»è¿½é®
xml_doc = minidom.parseString(xml_data)
对xml_docçå
容è¿è¡ä¿®æ¹
printâxml contentâï¼xml_doc.toxml( encoding='utf-8')
æå°xmlå
容 ç»æå°±æ¥é
xml content ï¼errorï¼attributeerrorï¼âintâ object has no attribute âreplaceâ
æåªéå¼éäºï¼
你的问题可以直接转化为python如何读取文件,使用这句代码:open('thefile.xml').read( )
下面是例子:
>>> open('D:\\Test.xml',encoding='utf8').read()不知道为什么,python3.4中读取文件的默认使用GBK,这一点要注意
那如果是elementtree呢?