English 中文(简体)
• 如何利用猪肉微型从XML中提取数据
原标题:How to extract data from XML using python minidom
  • 时间:2011-09-08 02:04:02
  •  标签:
  • python

鉴于这一xml文档,我谨从中提取数据。 然而,在<代码>和“LandmarkPointListXml>”上提取数据有困难;。

<>XML文档:

  <?xml version="1.0" encoding="utf-8"?>
  <Map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MapName>er</MapName>
  <MapURL>er.gif</MapURL>
  <Name>er</Name>
  <URL>er.gif</URL>
  <LandmarkPointListXml>
    <anyType xsi:type="LandmarkPointProperty">
      <LandmarkPointX>400</LandmarkPointX>
      <LandmarkPointY>292</LandmarkPointY>
      <LandmarkDesc>my room door</LandmarkDesc>
    </anyType>
    <anyType xsi:type="LandmarkPointProperty">
      <LandmarkPointX>399</LandmarkPointX>
      <LandmarkPointY>219</LandmarkPointY>
      <LandmarkDesc>bro room door</LandmarkDesc>
    </anyType>
  </LandmarkPointListXml>
  <RegionPointListXml />
</Map>

www.un.org/Depts/DGACM/index_spanish.htm 粉碎计划:

    def GetMapData(self):
        result = ""
        haha = self.XMLdoc.firstChild #root node
        for child in haha.childNodes:
            if (cmp(child.nodeName, LandmarkPointListXml )==0):
                result = result +  |  + self.loopLandmark(child.childNodes) +  | 
            else:
                result = result + child.firstChild.nodeValue +  , 
        return result

    def loopLandmark(self, landmarks):
        result=""
        haha=landmarks.getElementsByTagName( anyType )
        for child in haha.childNodes:
            if (cmp(haha.firstChild.nodeName, LandmarkPointX ) == 0):
                result=result+child.firstChild.nodeValue+ , 
                ChildNode = ChildNode.nextSibling
                result=result+child.firstChild.nodeValue+ , 
                ChildNode = ChildNode.nextSibling
                result=result+child.firstChild.nodeValue
        return result

我得以检索结果,即“er,er.gif,er,er.gif”,直到方案达到<LandmarkPointListXml>

最佳回答
问题回答

i 设法从公布的XML档案中提取数据。 但认为,这比提供答案更简单。 获取每项数据的通道。

import sys
import socket
import os
from xml.dom.minidom import Document, parse, parseString

class mapDataClass:

def __init__(self):
    self.XMLdoc = Document()
    self.MakeRootNode()

def MakeRootNode(self):
    self.RootNode = self.XMLdoc.createElement( Map )
    self.XMLdoc.appendChild(self.RootNode)

def GetXML_Doc(self):
    return self.XMLdoc

def LoadXMLFile(self, AbsFileName):
    try:
        self.XMLdoc.unlink()
        self.XMLdoc = parse(AbsFileName)
        if (self.XMLdoc.hasChildNodes()): #if not empty
            #Determine if root node <CalibrationData> exist
            if (cmp(self.XMLdoc.firstChild.nodeName,
                                 Map ) == 0):
                self.RootNode = self.XMLdoc.firstChild

        return True

    except IOError:
        print  File   + AbsFileName +   not found 
        return False

def GetMapData(self):
    result = ""
    haha = self.XMLdoc.firstChild #root node
    for child in haha.childNodes:
        if cmp(child.nodeName,  LandmarkPointListXml )==0:
            result1 = self.loopLandmark(child)
        elif cmp(child.nodeName,  RegionPointListXml )==0:
            print  Empty 
        elif cmp(child.nodeName,  URL )==0:
            result = result + child.firstChild.nodeValue
        else:
            result = result + child.firstChild.nodeValue +  , 
    result = result + "|" + result1 + "EMPTY"
    return result

def loopLandmark(self,landmarks):
    result2=""
    tempResult=""
    haha=landmarks.getElementsByTagName( anyType )
    for i in range(0, len(haha)):
        result2=self.loopAnyType(haha[i])
        if ((i+1)!=len(haha)):
            tempResult = tempResult + result2 +  ; 
        else:
            tempResult = tempResult + result2 +  | 
    return tempResult

def loopAnyType(self,anyType):
    result3=""
    haha1=anyType.getElementsByTagName( LandmarkPointX )[0]
    haha2=anyType.getElementsByTagName( LandmarkPointY )[0]
    haha3=anyType.getElementsByTagName( LandmarkDesc )[0]
    result3 = haha1.firstChild.nodeValue + "," + haha2.firstChild.nodeValue + "," + haha3.firstChild.nodeValue
    return result3

profile = mapDataClass()
boolean = profile.LoadXMLFile( uploader.m )
print boolean
result = profile.GetMapData()
print result

我先前的答复还不完整。 这里应认为是right。

import sys
import socket
import os
from xml.dom.minidom import Document, parse, parseString, Node

class mapDataClass:

def __init__(self):
    self.XMLdoc = Document()
    self.MakeRootNode()

def MakeRootNode(self):
    self.RootNode = self.XMLdoc.createElement( Map )
    self.XMLdoc.appendChild(self.RootNode)

def GetXML_Doc(self):
    return self.XMLdoc

def LoadXMLFile(self, AbsFileName):
    try:
        self.XMLdoc.unlink()
        self.XMLdoc = parse(AbsFileName)
        if (self.XMLdoc.hasChildNodes()): #if not empty
            if (cmp(self.XMLdoc.firstChild.nodeName,
                                 Map ) == 0):
                self.RootNode = self.XMLdoc.firstChild

        return True

    except IOError:
        print  File   + AbsFileName +   not found 
        return False

def GetMapData(self):
    result = ""
    result1 = ""
    result2 = ""
    haha = self.XMLdoc.firstChild #root node
    for child in haha.childNodes:
        if child.nodeType == Node.ELEMENT_NODE:
            if cmp(child.nodeName,  LandmarkPointListXml )<>0 and cmp(child.nodeName,  RegionPointListXml )<>0:
                if cmp(child.nodeName,  URL )==0:
                    result = result + child.firstChild.nodeValue       
                else:
                    result = result + child.firstChild.nodeValue +  , 
            elif cmp(child.nodeName,  LandmarkPointListXml )==0:
                if child.firstChild is not None:
                    result1 = self.loopLandmark(child)
                else:
                    result1 =  EMPTY| 
            elif cmp(child.nodeName,  RegionPointListXml )==0:
                if child.firstChild is None:
                    result2 =   EMPTY 

    result = result + "|" + result1 + result2
    return result

def loopLandmark(self,landmarks):
    result2=""
    tempResult=""
    haha=landmarks.getElementsByTagName( anyType )
    for i in range(0, len(haha)):
        result2=self.loopAnyType(haha[i])
        if ((i+1)!=len(haha)):
            tempResult = tempResult + result2 +  ; 
        else:
            tempResult = tempResult + result2 +  | 
    return tempResult

def loopAnyType(self,anyType):
    result3=""
    haha1=anyType.getElementsByTagName( LandmarkPointX )[0]
    haha2=anyType.getElementsByTagName( LandmarkPointY )[0]
    haha3=anyType.getElementsByTagName( LandmarkDesc )[0]
    result3 = haha1.firstChild.nodeValue + "," + haha2.firstChild.nodeValue + "," + haha3.firstChild.nodeValue
    return result3

data = mapDataClass()
success = data.LoadXMLFile("uploadhomeTest.m")
if success:
    print "file loaded"
    print data.GetMapData()
else:
    print "no such file found"




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签