PythonでDOM(Document Object Model)を使ってXMLファイルからデータを読み込む例です。この例ではJava, C++, Pythonのそれぞれのprint関数をリストアップします。
sample.xml
source code.
output
Reference
DOM(Python版)入門
This is example code for reading XML by DOM(Document Object Model) in Python. In this example, it list up print function in Java, C++ and Python for each rule.
Reading XML in Python is very easy!
Reading XML in Python is very easy!
sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<CommandList>
<cmd type="Java">System.out.print("test")</cmd>
<cmd type="C++">printf("test")</cmd>
<cmd type="Python">print("test")</cmd>
</CommandList>
source code.
from xml.dom.minidom import parse def printCommandList(xmlPath):
dom = parse(xmlPath);
root = dom.documentElement
print(root.nodeName);
cmdNodeList = root.childNodes;
for cmdNode in cmdNodeList:
if cmdNode.nodeName == "cmd":
typeName = cmdNode.getAttributeNode("type");
command = "";
cmdText = cmdNode.childNodes[0];
if cmdText.nodeType == cmdText.TEXT_NODE:
command = cmdText.data;
print(str(typeName.nodeValue)+":"+str(command));
dom.unlink();
printCommandList("./sample.xml");
output
CommandList
Java: System.out.print("test")
C++: printf("test")
Python: print("test")
Java: System.out.print("test")
C++: printf("test")
Python: print("test")
Reference
DOM(Python版)入門