❶ 1、使用python讀取依據生成的xml文件,添加樣式表,最中生成一個html文件
#coding=utf8
#引入要用到的xml解析庫這里我們用比較輕量級的minidom就行了
importxml.dom.minidom
#定義一個html輸出模板
#後面我們只是要把這段html中的學生數據部分(<student_trs/>)換成xml中讀到的數據
template="""
<html>
<tableborder="1"style="width:100%;text-align:center;">
<tr>
<tdcolspan="4">學生信息</td>
</tr>
<student_trs/>
</table>
</html>
"""
#讀取xml文檔內容,這里假設是a.xml
dom=xml.dom.minidom.parse('a.xml')
#獲取xml中的所有student節點
student_nodes=dom.getElementsByTagName('student')
#初始化student_trs為空
student_trs=""
#遍歷每一條學生信息
fornodeinstudent_nodes:
#getAttribute用戶獲取節點的屬性,得到id屬性值即學號
#因為xml解析後是Unicode編碼的,所以這里要轉成utf8編碼,下面同理
sid=node.getAttribute("id").encode('utf-8')
#獲取所有子節點
children=node.childNodes
forchildinchildren:
#判斷子節點的名字為姓名、性別、專業的話,就採集其對應文本
ifchild.nodeName.encode('utf-8')=="姓名":
#使用。childNodes[0].nodeValue的方法得到節點的文本
name=child.childNodes[0].nodeValue.encode('utf-8')
ifchild.nodeName.encode('utf-8')=="性別":
sex=child.childNodes[0].nodeValue.encode('utf-8')
ifchild.nodeName.encode('utf-8')=="專業":
specialty=child.childNodes[0].nodeValue.encode('utf-8')
#組成html中的一行學生數據
student_tr="<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>"%(sid,name,sex,specialty)
#將這一行數據添加到總數據中
student_trs+=student_tr
#替換模板的<student_trs/>為我們上面所得到的html數據
html=template.replace("<student_trs/>",student_trs)
#輸入html結果到output.html
open("output.html","w").write(html)
#PS:你提供的xml數據有問題,確實了一個</students>標記
#正確的xml應該如下
"""
<?xmlversion="1.0"encoding="UTF-8"?>
<person>
<students>
<studentid="20140711">
<姓名>三</姓名>
<性別>男</性別>
<專業>計算機</專業>
</student>
</students>
</person>
"""
❷ python xml 查找關鍵字
可以這么寫:
def find(html):
soup = BeautifulSoup(html.getResponse().content,from_encoding='gb18030')
for index in soup.find_all('ROWDATA'):
print index
html是你的網頁域名
❸ python讀取xml文件有哪些方法
1、以下幾種方法建議初學者使用:
xml.etree.ElementTree
xml.dom
xml.dom.minidom
xml.dom.pulldom
xml.parsers.expat
其中,第一個模塊更加輕便簡介,對於簡單的xml文檔,推薦使用
下面的方法涉及知識比較多,熟練掌握上面方法後可以了解使用:
2、Dom讀取
3、Dom4j讀取
使用dom4j需要導入相關的jar包
import java.io.File;
import java.util.Iterator;
import java.util.List;
4、JDom讀取
使用jdom需要導入相關的jar包
import java.io.FileInputStream;
import java.io.InputStream;
5、Sax讀取
6、properties的讀取
❹ 如何用Python創建生成xml文檔文件的方法
1、內存數據產生
2、產生xml內存對象(也就是DOM樹)
3、產生根對象
4、往根對象里加數據
5、把xml內存對象寫到文件
下面是一個創建xml文檔的簡單實例:
importxml.dom.minidom#在內存中創建一個空的文檔doc=xml.dom.minidom.Document()
#創建一個根節點Managers對象root=doc.createElement('Managers')
#設置根節點的屬性root.setAttribute('company','xx科技')
root.setAttribute('address','科技軟體園')
#將根節點添加到文檔對象中doc.appendChild(root)
managerList=[{'name':'joy','age':27,'sex':'女'},
{'name':'tom','age':30,'sex':'男'},
{'name':'ruby','age':29,'sex':'女'}
]foriinmanagerList:
nodeManager=doc.createElement('Manager')
nodeName=doc.createElement('name')
#給葉子節點name設置一個文本節點,用於顯示文本內容
nodeName.appendChild(doc.createTextNode(str(i['name'])))
nodeAge=doc.createElement("age")
nodeAge.appendChild(doc.createTextNode(str(i["age"])))
nodeSex=doc.createElement("sex")
nodeSex.appendChild(doc.createTextNode(str(i["sex"])))
#將各葉子節點添加到父節點Manager中,
#最後將Manager添加到根節點Managers中
nodeManager.appendChild(nodeName)
nodeManager.appendChild(nodeAge)
nodeManager.appendChild(nodeSex)
root.appendChild(nodeManager)#開始寫xml文檔fp=open('c:\wcx\Manager.xml','w')
doc.writexml(fp,indent=' ',addindent=' ',newl=' ',encoding="utf-8")
執行結果:
<?xmlversion="1.0"encoding="utf-8"?>
<Managersaddress="科技軟體園"company="xx科技">
<Manager>
<name>joy</name>
<age>27</age>
<sex>女</sex>
</Manager>
<Manager>
<name>tom</name>
<age>30</age>
<sex>男</sex>
</Manager>
<Manager>
<name>ruby</name>
<age>29</age>
<sex>女</sex>
</Manager>
</Managers>
6.用Python自帶的寫xml文檔的API去寫,比較方便,後期容易維護。如果直接用打開文件的方式,一行一行的去寫,比較費時,也難以維護。