XML-құ ұ ү XML- қ қ. Document ң құ XML-құ parse () ө . , xml қ PyXML қ қғ . құ DOM Level 2 ққ, API ұғ .
import xml.dom.minidom
dom = xml.dom.minidom.parse("expression.xml")
dom.normalize()
def output_tree(node, level=0):
if node.nodeType == node.TEXT_NODE:
if node.nodeValue.strip():
print ". "*level, node.nodeValue.strip()
else: # ELEMENT_NODE DOCUMENT_NODE
atts = node.attributes or {}
att_string = ", ".join(
["%s=%s " % (k, v) for k, v in atts.items()])
print ". "*level, node.nodeName, att_string
for child in node.childNodes:
output_tree(child, level+1)
output_tree(dom)
ұ ғ, ғ қ ә қ ғ ү қ, қ output_tree() ө ғ.
ә ғ:
#document
. expression
.. operation type=+
... operand
.... 2
... operand
.... operation type=*
..... operand
...... 3
..... operand
...... 4
қ ә ү, ұ normalize() ә қ (ә қ ә ү ә ү).
ҳ ғ ғ қғ қғ : node.nodeType ғ ү ө, node.nodeValue ә қ ү қ, node.nodeName ғ ( ә), node.attributes ғ қ . node.childNodes ( ) ғғ қ ү қ. қ ғ ө ү .
қ ғ Node ң ң . ү ү:
ғ | Ә құ ү | |
ELEMENT_NODE | createElement(tagname) | |
createElement(tagname) | createAttribute(name) | |
TEXT_NODE | ә ғ | createTextNode(data) |
CDATA_SECTION_NODE | CDATA ө | |
ENTITY_REFERENCE_NODE | ѳ ә | |
ENTITY_NODE | ә | |
PROCESSING_INSTRUCTION_NODE | ұққ өң | createProcessingInstruction(target, data) |
COMMENT_NODE | ϳ | createComment(comment) |
DOCUMENT_NODE | Құ | |
DOCUMENT_TYPE_NODE | Құ ү | |
DOCUMENT_FRAGMENT_NODE | Құ | |
NOTATION_NODE |
|
|
ғғ қ ү ұ DOM құ ғ . ғ ү. ғғ қ childNodes ( ү), firstChild ( ү), lastChild (ңғ ү), (-), nextSibling ( ғ),previousSibling (ңғ ғ).parentNode қ ү ғ .
ғ appendChild() ә қ . ғ insertBefore(newChild, refChild) (refChild- newChild қ), removeChild(oldChild)
( ү ө), replaceChild(newChild, oldChild) ( oldChild newChild) ә қғ . ғ ғ ( deep=1 , ү ), cloneNode(deep) ә .
ELEMENT_NODE ғ ү, ғ қ ә " ғ" ң ө қ ә . ̳ ң :
tagName
үң .
getElementsByTagName(tagname)
қ ң үң tagname ө .
getAttribute(attname)
attname ә .
getAttributeNode(attrname)
-ғ ү attrname қ.
removeAttribute(attname)
attname ө.
removeAttributeNode(oldAttr)
oldAttr ө (-ғ ү )
setAttribute(attname, value)
value ң attname ң ә .
setAttributeNode(newAttr)
ң ғ- . қ , .
ұ , ң қ . ң ә, -қ XML ққ ң ұғ ң .
ғ XML-ұ өң ә ғ құ ұ.
ң
XML-ң ғ ң . әү ң XML-құ құғ ү . , , қ HTML ң ң ө, XML-құ HTML қғ .
XML-ң ң ң ө (foaf.rdf ):
<?xml version="1.0" encoding="UTF-8"?>
|
|
<rdf:RDF
xmlns:dc="http://http://purl.org/dc/elements/1.1/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
<rdf:Description rdf:nodeID="_:jCBxPziO1">
<foaf:nick>donna</foaf:nick>
<foaf:name>Donna Fales</foaf:name>
<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
</rdf:Description>
</rdf:RDF>
:
WWW , - ғ құ құғ cwm ғ. қ, cwm- Python ғ. cwm , --ң қғ, қ -ң ү қғ өң қ .
ң ң ң ң ү ү. ұ ғ . URI (Universal Resource Locator, ә ө) ү ә. ғ ң ғ (xmlns, dc, rdfs, foaf rdf), ң қ , ө қғ . ә қ ү ү ғ қғ: (xmlns, foaf ә rdf).
ң XML-құ ү ғ ө ө ү .
Xml ң ң ү ә . Ә ұ ә ә өң NS ә .
ң ң ә URI- namespaceURI ң ө ғ .
URI :
import xml.dom.minidom
dom = xml.dom.minidom.parse("ex.xml")
def output_ns(node):
if node.nodeType == node.ELEMENT_NODE:
print node.nodeName, node.namespaceURI
for child in node.childNodes:
output_ns(child)
output_ns(dom)
ә:
rdf:RDF http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdf:Description http://www.w3.org/1999/02/22-rdf-syntax-ns#
foaf:nick http://xmlns.com/foaf/0.1/
foaf:name http://xmlns.com/foaf/0.1/
rdf:type http://www.w3.org/1999/02/22-rdf-syntax-ns#
, ң ң ұқ ү ғ , қ ғ ү.
ұ ә ү ғ ә қ өңң ұқ қ: CSV, Unix mailbox ә XML. Ә , ә ғ ө, ұғ ә, өң ү қ ү ө құ ұғғ ө.