Need Python-XML help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • magestic
    New Member
    • Mar 2007
    • 1

    #1

    Need Python-XML help

    Hello,

    I'm stuck writing a piece of software. I'm using Python 2.5 on WinXP Pro. I don't know how to get done what I need to do. I have some standardized input that looks like this:

    Code:
    <somename>
    <modulename>
    <item>
    <id>#UNIQUE_ID#</id>
    <datetime>yyyy-MM-dd hh:mm:ss</datetime>
    <title>#TEXT TITLE#</title>
    <description>#TEXT BODY#</description>
    <link>#LINK TO ARTICLE#</link>
    </item>
    </modulename>
    </somename>
    This is the output of a parserprogram I'm working with. What I want to do is write these <items> into seperate files, where the filenames is the #UNIQUE ID#. I want to write these little files into a map with the date in it. So for a date that is 22/03/07 I want the item-files in 220307\<item>.x ml .
    I've got no clue how to do this. I can copy the items to a new XML tree using ElementTree, but can't figure out how to extract the elements for the XML and use it in file/map naming. Could anybody give me some pointers?
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    you can use XML parsers that makes things easier for you,but I use regexp parsing ( or simple string manipulations) for this case.
    As i don't understand the rest of the question, this little piece of code just get out the necessary information that you need between the "item" tag.
    Code:
    data = open("file").read()
    pattern = re.compile("<item>(.*?)</item>",re.M|re.DOTALL)
    for items in pattern.findall(data):
        filename = re.findall("<id>(.*?)</id>",items)[0]
        datetiming = re.findall("<datetime>(.*?)</datetime>",items)[0]
        title = re.findall("<title>(.*?)</title>",items)[0]
        desc = re.findall("<description>(.*?)</description>",items)[0]
        link = re.findall("<link>(.*?)</link>",items)[0]
    print filename
    print datetiming
    print title
    print desc
    print link

    Comment

    Working...