mojonapower
mojonapower

Reputation: 31

How can i get a value in XML file? Python

I can find elements but i want get the value of the tag This is my xml file.

<?xml version="1.0" encoding="UTF-8"?><BusinessTransactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Controller="http://10.43.11.143:8090/controller/rest" GenerationTime="2015-12-30T09:47:46.817698-03:00" xsi:noNamespaceSchemaLocation="bt_metrics.xsd"> 
 <BusinessTransaction>
<ApplicationName>Portales</ApplicationName>
<BusinessTransactionName>APP</BusinessTransactionName>
<AverageResponseTime>142</AverageResponseTime>      
<CallsPerMinute>169</CallsPerMinute>
<ErrorsPerMinute>15</ErrorsPerMinute>

and my code

from xml.dom.minidom import parse, parseString
from xml.dom import minidom
dom = parse("data.xml")
for node in dom.getElementsByTagName('CallsPerMinute'): 
print node.toxml()

Upvotes: 2

Views: 41

Answers (1)

CodeLikeBeaker
CodeLikeBeaker

Reputation: 21312

You need to use the firstChild.nodeValue in order to get the value of the node:

from xml.dom.minidom import parse

dom = parse("data.xml")
for node in dom.getElementsByTagName('CallsPerMinute'):
    print(node.firstChild.nodeValue)

Also, your xml needs your tags closed:

<?xml version="1.0" encoding="UTF-8"?>
<BusinessTransactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      Controller="http://10.43.11.143:8090/controller/rest"
                      GenerationTime="2015-12-30T09:47:46.817698-03:00" xsi:noNamespaceSchemaLocation="bt_metrics.xsd">
    <BusinessTransaction>
        <ApplicationName>Portales</ApplicationName>
        <BusinessTransactionName>APP</BusinessTransactionName>
        <AverageResponseTime>142</AverageResponseTime>
        <CallsPerMinute>169</CallsPerMinute>
        <ErrorsPerMinute>15</ErrorsPerMinute>
    </BusinessTransaction>
</BusinessTransactions>

Upvotes: 3

Related Questions