views:

42

answers:

1

Hi,

I have one xml file.

<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Salary</GroupName>
<EditId>undefined</EditId>

<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Salary</GroupName>
<EditId>undefined</EditId>

<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Trfr fm Savings</GroupName>
<EditId>undefined</EditId>

<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Dividend</GroupName>
<EditId>undefined</EditId>

<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Dividend</GroupName>
<EditId>undefined</EditId>

Now i want to get the all item,itemdate, etc are seperately using elementtree. Any one can help me?

Rgds,

Nimmy

+2  A: 

As sje397 wrote in the comment, you should restructure it if you have an option. Either put it all into item tags:

<item>
    <value>...</value>
    <date>...</date>
    ...
</item>

Or using attributes:

<item value="..." date="..." ... />

Those are largely equivalent (attributes an appear in any order though, while tags can be forced into a certain order via Schema/DTD) with the exception that you have to and I think it's a matter of taste. Of course you can mix the two, but that will complicate extracting the information (as you'll need to use seperate methods to get attributes vs tags). Either way, you just get one item tag and then get all its [children|attributes].

If the xml absolutely has to stay this way, you might want to look into SAX parsers, which inherently preserve the order of the tags. It requires an event-based approach though.

delnan