0

I have this XML:

<SwitchPort name="1110" portType="RS422_Board2">
<Bus>UO-2-3</Bus>
<Device>FC3</Device>
<Appearance>Simulated</Appearance>
</SwitchPort>

I can get the portType using:

for node in NCFile.getElementsByTagName("SwitchPort"):
    portType = node.getAttribute("portType")

But I want to get the values of Apperance, Device, etc.

What am I doing wrong?

Matt

2 Answers 2

1

Simply use getElementsBytagName on your node and get the nodeValue

for node in NCFile.getElementsByTagName("SwitchPort"):
    portType = node.getAttribute("portType")
    device = node.getElementsByTagName("Device")[0].firstChild.nodeValue
    appearance = node.getElementsByTagName("Appearance")[0].firstChild.nodeValue
Sign up to request clarification or add additional context in comments.

3 Comments

for node in NCFile.getElementsByTagName("SwitchPort"): portType = node.getAttribute("portType") print(portType) #device = node.getElementsByTagName("Device")[0].firstChild.nodeValue #appearance = node.getElementsByTagName("Appearance")[0].firstChild.nodeValue #print(appearance)
Thank you Rafael: But now I just get a limited number of the data that I want. for node in NCFile.getElementsByTagName("SwitchPort"): portType = node.getAttribute("portType") print(portType) #device = node.getElementsByTagName("Device")[0].firstChild.nodeValue #appearance = node.getElementsByTagName("Appearance")[0].firstChild.nodeValue #print(appearance)
@M.Pickard sorry, its hard to understand your comments? Could you edit the post ?
0

you can use lxml for parsing xml - lxml

from lxml import etree

root_element = etree.fromstring(xml_string)

device_node_value = root_element.findtext('Device')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.