LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2012, 09:38 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
CLI: need python program to handle XML contacts


Hi,

need python program to handle XML contacts under Console (CLI, no X11, DISPLAY="")

The format looks like this, and please find a sample:

cat .osmo/contacts_records.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<osmo_contacts>
  <contacts_groups/>
  <contacts_records>
    <record>
      <group>None</group>
      <first_name>first name linux</first_name>
      <last_name>last name linux</last_name>
      <nickname>Nick linux</nickname>
      <birthday_date>0</birthday_date>
      <name_day_date>0</name_day_date>
      <home_address>address </home_address>
      <home_postcode>postcode linux</home_postcode>
      <home_city>city in the world</home_city>
      <home_phone_1>home phone nr 4334554</home_phone_1>
      <work_phone_1>work phone nr  435454</work_phone_1>
      <cell_phone_1>43545344243</cell_phone_1>
      <email_1>linux@gmail.com</email_1>
      <www_1>www.linux.com</www_1>
      <im_skype>my skype pseudo</im_skype>
      <additional_info>additional info 

this field is important</additional_info>
    </record>
    <record>
      <group>None</group>
      <first_name>second contact first name </first_name>
      <last_name>last name</last_name>
      <nickname>nickname</nickname>
      <birthday_date>0</birthday_date>
      <name_day_date>0</name_day_date>
      <home_address> my address</home_address>
      <home_postcode>postcode </home_postcode>
      <home_city>city</home_city>
      <home_phone_1>45345435</home_phone_1>
    </record>
  </contacts_records>
</osmo_contacts>
Would you have a simple python code that could :
- add
- edit
those categories mentioned above in this sample?
thank you in advance a lot lot if it might be possible for an experienced coder.

Last edited by Xeratul; 05-10-2012 at 10:08 PM.
 
Old 05-10-2012, 10:02 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Firstly, don't put "urgent" in your thread titles. What're urgent to me are my own problems, not yours

What sort of thing are you looking for? Do you mean an interactive programme which will allow you to browse through and amend contact details, or just a script that will do something predefined to all the contacts? If you're a bit more specific, then we should be able to help you
 
Old 05-10-2012, 10:29 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Exactly what Snark1994 said.

Xeratul, if you want an interactive program (as opposed to a batch processing tool), do you want a command-line application, or a GUI one? Which toolkit to use? PyQt4?

There are basically two approaches to editing:
  1. Parse the entire dataset into an abstract data structure (associative arrays in Python) in memory, edit, then emit the entire structure as XML
  2. Read the entire dataset as text, editing only the fields, keeping everything else in both that record and all other records intact
    (When adding a new record, use the first record as a template)
The first is easier to implement, but often results in loss of fidelity if extraneous data (especially attributes) are added to the XML file by some other utility. The second one is extremely useful in that it edits only field contents and keeps any extraneous data intact, but it is also a lot more difficult to implement, as you basically need to implement an XML parser yourself.
 
Old 05-10-2012, 10:49 AM   #4
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 360

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by Nominal Animal View Post
Parse the entire dataset into an abstract data structure (associative arrays in Python) in memory, edit, then emit the entire structure as XML
For this xml.etree.ElementTree from Python's standard library or lxml are great choices. They parse XML into an object tree and provide various ways to traverse and manipulate it. Both have tutorials and should be easy to start using.
 
1 members found this post helpful.
Old 05-10-2012, 11:08 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by audriusk View Post
For this xml.etree.ElementTree from Python's standard library or lxml are great choices.
Agreed. (By the associative arrays I meant the structures you'd use internally in the application, extracted from the ElementTree in this case. Accessing the data dynamically via the ElementTree would be a bit cumbersome for e.g. search functionality.)

The Python 2 vs. Python 3 differences are a bit annoying in this regard, however; so do consider whether you wish to use Python 2 or Python 3, before selecting the library, and starting the implementation.

In my opinion, Python 2.7 + PyQt4 + etree would be my choice for a cross-platform utility -- only requirements being Python 2.7 + PyQt4, and both are available in easy packages for practically all platforms --, but I'd be aware of the need to transition to Python 3 in a few years. In a corporate-type environment where the Python 3 packages are guaranteed to be installed, I'd go with Python 3 instead.

Last edited by Nominal Animal; 05-10-2012 at 11:09 AM.
 
Old 05-10-2012, 10:10 PM   #6
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by Snark1994 View Post
Firstly, don't put "urgent" in your thread titles. What're urgent to me are my own problems, not yours

What sort of thing are you looking for? Do you mean an interactive programme which will allow you to browse through and amend contact details, or just a script that will do something predefined to all the contacts? If you're a bit more specific, then we should be able to help you
Thank you for your improvements. I have changed the title, and content. It is indeed better.


For browsing indeed. A good example would be abook where one can browse and edit the contacts. I may post a screenshot: http://upload.wikimedia.org/wikipedi...e/e8/Abook.png


"For this xml.etree.ElementTree from Python's standard library or lxml are great choices. " sounds relatively comfortable methods...

Computing / Thinking how... http://www.ericwestbrook.com/images/...g?s=1181746040

Last edited by Xeratul; 05-10-2012 at 10:14 PM.
 
Old 05-11-2012, 05:17 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
The screenshot you posted looks like it uses the curses module for python, a tutorial for which is here. If you have any problems, post back here and we'll be glad to help you
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Light GTK and CLI software to handle Contacts? Xeratul Linux - Software 1 01-30-2012 04:36 PM
python: how to handle unicode chars in ascii strings? BrianK Programming 1 01-26-2009 09:46 PM
Program to Backup Contacts on Razr V3i taurusx5 Linux - Software 3 02-28-2008 12:39 AM
LXer: Jigsaw Business Contacts Marketplace Soars Past 3 Million Contacts ... LXer Syndicated Linux News 0 07-18-2006 07:54 AM
linux program to handle xml like Internet Explorer? exitsfunnel Linux - Software 3 10-31-2003 08:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration