[Tutor] Trouble Parsing XML using lxml

marc at marcd.org marc at marcd.org
Sat Apr 4 03:05:22 CEST 2009


Hi - I'm hoping to get some guidance

I am trying to parse a structure that looks like:

{urn:FindingImport}TOOL - GD
{urn:FindingImport}TOOL_VERSION - 2.0.8.8
{urn:FindingImport}AUTHENTICATED_FINDING - TRUE
{urn:FindingImport}GD_VUL_NAME - Rename Built-in Guest Account
{urn:FindingImport}GD_SEVERITY - 2
{urn:FindingImport}FINDING - None
{urn:FindingImport}FINDING_ID - V0001115
{urn:FindingImport}FINDING_STATUS - NF
{urn:FindingImport}TOOL - GD
{urn:FindingImport}TOOL_VERSION - 2.0.8.8
{urn:FindingImport}AUTHENTICATED_FINDING - TRUE
{urn:FindingImport}GD_VUL_NAME - Rename Built-in Administrator Account
{urn:FindingImport}GD_SEVERITY - 2
{urn:FindingImport}FINDING - None
{urn:FindingImport}FINDING_ID - V0001117

This is the result when the original data is run through 'for element in
root.iter():' as described in the lxml tutorial.  This structure repeats
many times in the document with different values after each tag.  I want
to take the values and place them in one csv line for each structure in
the file.  The closest I have come is something like (but doesn't work):

    for element in root.iter("{urn:FindingImport}TOOL"):
        print element.text
        print element.getnext().text
        print element.getnext().text

The initial print element.tag and the first element.getnext().text work as
I would like, but I am not finding a way to parse past that.  The second
element.getnext().text returns the value for the same tag as the one prior
to it.  I know I am missing something, but don't see it.  Any assistance
is appreciated.

Thanks,

marc




More information about the Tutor mailing list