简体   繁体   中英

Print xml tags correctly

<?xml version="1.0" encoding="utf8"?>
 <Test xmlns="http://schemas.microsoft.com/developer/build/2003">
 <Group>
 <Project Include="Name">    
      <Node1>true</Node1>
      <Node2>true</Node2>
      <Node3>false</Node3>
      <Node4>D3A4E4D9-C9E4-4f31-A225-E10A48AA5E10</Node4>
      <Node5>D3A4E4D9-C9E4-4f31-A225-E10A48AA5E10</Node5>
      <Node6>C6311C26-274C-40ea-8DF8-34F80F00DED3</Node6>
      <Node7>C6311C26-274C-40ea-8DF8-34F80F00DED3</Node7>
      <Node8>128</Node8>
      <Node9>128</Node9>
      <Node10>1000</Node10>
      <Node11>255</Node11>
      <Node12>0</Node12>
      <Node13>128</Node13>
      <Node14>007</Node14>

  </Project>
 </Group>

I have an xml file as given above.I want to get the Tag and Text of all nodes under Project node.I wrote following code to get the Node and text value .But it is not working

tree = ElementTree(file= filename)
root = tree.getroot()
for Group in root:
for Project in Group:
    for child in Project:
        print child.tag,child.text

But when i print iam getting the output as given below.How can i remove { http://schemas.microsoft.com/developer/build/2003 } from my output

{http://schemas.microsoft.com/developer/build/2003}Node1 true
{http://schemas.microsoft.com/developer/build/2003}Node2 false
{http://schemas.microsoft.com/developer/build/2003}Node3 D3A4E4D9-C9E4-4f31-A225-   E10A48AA5E10
{http://schemas.microsoft.com/developer/build/2003}Node4 D3A4E4D9C9E44f31A225E10A48AA5E10
{http://schemas.microsoft.com/developer/build/2003}Node5 C6311C26-274C-40ea-8DF8-34F80F00DED3
{http://schemas.microsoft.com/developer/build/2003}Node6 C6311C26-274C-40ea-8DF8-34F80F00DED
{http://schemas.microsoft.com/developer/build/2003}Node7 128
{http://schemas.microsoft.com/developer/build/2003}Node8 128
{http://schemas.microsoft.com/developer/build/2003}Node9 1000
{http://schemas.microsoft.com/developer/build/2003}Node10 255
{http://schemas.microsoft.com/developer/build/2003}Node11 0
{http://schemas.microsoft.com/developer/build/2003}Node12 128
{http://schemas.microsoft.com/developer/build/2003}Node13 007

Split the name by } and take the last part.

tree = ElementTree(file=filename)
root = tree.getroot()
for Group in root:
    for Project in Group:
        for child in Project:
            print child.tag.split('}')[-1], child.text

output:

Node1 true
Node2 true
Node3 false
Node4 D3A4E4D9-C9E4-4f31-A225-E10A48AA5E10
Node5 D3A4E4D9-C9E4-4f31-A225-E10A48AA5E10
Node6 C6311C26-274C-40ea-8DF8-34F80F00DED3
Node7 C6311C26-274C-40ea-8DF8-34F80F00DED3
Node8 128
Node9 128
Node10 1000
Node11 255
Node12 0
Node13 128
Node14 007

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM