469,299 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. <somename>
  2. <modulename>
  3. <item>
  4. <id>#UNIQUE_ID#</id>
  5. <datetime>yyyy-MM-dd hh:mm:ss</datetime>
  6. <title>#TEXT TITLE#</title>
  7. <description>#TEXT BODY#</description>
  8. <link>#LINK TO ARTICLE#</link>
  9. </item>
  10. </modulename>
  11. </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>.xml .
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?
Mar 26 '07 #1
1 1060
ghostdog74
511 Expert 256MB
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.
Expand|Select|Wrap|Line Numbers
  1. data = open("file").read()
  2. pattern = re.compile("<item>(.*?)</item>",re.M|re.DOTALL)
  3. for items in pattern.findall(data):
  4.     filename = re.findall("<id>(.*?)</id>",items)[0]
  5.     datetiming = re.findall("<datetime>(.*?)</datetime>",items)[0]
  6.     title = re.findall("<title>(.*?)</title>",items)[0]
  7.     desc = re.findall("<description>(.*?)</description>",items)[0]
  8.     link = re.findall("<link>(.*?)</link>",items)[0]
  9. print filename
  10. print datetiming
  11. print title
  12. print desc
  13. print link
  14.  
Mar 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Graeme Matthew | last post: by
3 posts views Thread by Graham Nicholls | last post: by
21 posts views Thread by Chris Reedy | last post: by
9 posts views Thread by Roy Smith | last post: by
10 posts views Thread by Jeff Wagner | last post: by
2 posts views Thread by Aaron | last post: by
10 posts views Thread by blah | last post: by
14 posts views Thread by mistral | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.