0

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

1
  • 1
    If you have an option to restructure that XML you should. XML like that should be nested, with the properties of each item appearing within each <Item> element. Commented Jul 24, 2010 at 10:23

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

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.