ضع فى حسبانك ملف كالتالى
<?xml version="1.0"?>
<computer>
<library>
<books>
<book id="1">
<name>Introduction to Python</name>
<author>Ahmed Youssef</author>
<price>80</price>
</book>
<book id="2">
<name>Introduction to Java</name>
<author>Wael Muhammed</author>
<price>130</price>
</book>
<book id="3">
<name>Introduction to Ruby</name>
<author>Ahmed Youssef</author>
<price>70</price>
</book>
<book id="4">
<name>Introduction to Linux Programming</name>
<author>Ahmed Mostafa</author>
<price>90</price>
</book>
</books>
</library>
</computer>
فاكر الموضوع الممل عن معالجة ملفات ال XML فى بايثون ؟
http://programming-fr34ks.net/smf/articles-12/xmling-with-python/ DOM, SAX وكلام كتير مارأيك فى هذا الكود ؟
for book in computer.library.books:
print book.id, book.name, ", by ", book.author
totalsum=sum([int(str(book.price)) for book in computer.library.books])
print "SUM: ", totalsum
مش اسهل بكتير ؟
الناتج
1 Introduction to Python , by Ahmed Youssef
2 Introduction to Java , by Wael Muhammed
3 Introduction to Ruby , by Ahmed Youssef
4 Introduction to Linux Programming , by Ahmed Mostafa
SUM: 370
ماذا عن ال attributes ؟
تستطيع الوصول اليها ايضا من خلال اسمها مباشرة
او ملف من amazon مشابه للتالى
<?xml version="1.0" encoding="UTF-8"?>
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<OperationRequest>
<HTTPHeaders>
<Header Name="UserAgent">
</Header>
</HTTPHeaders>
<RequestId>16WRJBVEM155Q026KCV1</RequestId>
<Arguments>
<Argument Name="SearchIndex" Value="Books"></Argument>
<Argument Name="Service" Value="AWSECommerceService"></Argument>
<Argument Name="Title" Value="Ruby on Rails"></Argument>
<Argument Name="Operation" Value="ItemSearch"></Argument>
<Argument Name="AWSAccessKeyId" Value="dontbeaswoosh"></Argument>
</Arguments>
<RequestProcessingTime>0.064924955368042</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
<IsValid>True</IsValid>
<ItemSearchRequest>
<SearchIndex>Books</SearchIndex>
<Title>Ruby on Rails</Title>
</ItemSearchRequest>
</Request>
<TotalResults>22</TotalResults>
<TotalPages>3</TotalPages>
<Item>
<ASIN>0321480791</ASIN>
<DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=0321480791%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0321480791%253FSubscriptionId=dontbeaswoosh</DetailPageURL>
<ItemAttributes>
<Author>Michael Hartl</Author>
<Author>Aurelius Prochazka</Author>
<Manufacturer>Addison-Wesley Professional</Manufacturer>
<ProductGroup>Book</ProductGroup>
<Title>RailsSpace: Building a Social Networking Website with Ruby on Rails (Addison-Wesley Professional Ruby Series)</Title>
</ItemAttributes>
</Item>
</Items>
</ItemSearchResponse>
وتريد الحصول على بعض العناصر اليس كذلك ؟
print rt.OperationRequest.HTTPHeaders.Header.Name
print rt.OperationRequest.Arguments[0].Name
print rt.OperationRequest.RequestProcessingTime
print rt.Items.TotalPages
print rt.Items.TotalResults
print rt.Items.Item.ASIN
print rt.Items.Item.DetailPageURL
اليس اسهل كثيرا ؟
UserAgent
SearchIndex
0.064924955368042
3
22
0321480791
http://www.amazon.com/gp/redirect.html%3FASIN=0321480791%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0321480791%253FSubscriptionId=dontbeaswoosh
او ربما مهتم ب Twitter ؟
على فرض لدينا هذا الملف
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<created_at>Sat Aug 09 05:38:12 +0000 2008</created_at>
<id>882281424</id>
<text>I so just thought the guy lighting the Olympic torch was falling when he began to run on the wall. Wow that would have been catastrophic.</text>
<source>web</source>
<truncated>false</truncated>
<in_reply_to_status_id>1234</in_reply_to_status_id>
<in_reply_to_user_id>12345</in_reply_to_user_id>
<favorited></favorited>
<user>
<id>4243</id>
<name>John Nunemaker</name>
<screen_name>jnunemaker</screen_name>
<location>Mishawaka, IN, US</location>
<description>Loves his wife, ruby, notre dame football and iu basketball</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/53781608/Photo_75_normal.jpg</profile_image_url>
<url>http://addictedtonew.com</url>
<protected>false</protected>
<followers_count>486</followers_count>
</user>
</status>
</statuses>
وتريد الحصول على كل ماتحت ال user
statuses.status.user.inspect_me()
ستجد الناتج
Attrs:
Tags:
id => 4243
name => John Nunemaker
screen_name => jnunemaker
location => Mishawaka, IN, US
description => Loves his wife, ruby, notre dame football and iu basketball
profile_image_url => http://s3.amazonaws.com/twitter_production/profile_images/53781608/Photo_75_normal.jpg
url => http://addictedtonew.com
protected => false
followers_count => 486
اكيد مازالت تحتاج للعديد من الإختبارات وبإستخدامك هيتم تحسينها
كيفية الإستخدام
1- استدعاء happymapper
2- استخدام الدالة get_root اللتى تأخذ معامل xmlfile وهو مسار ملف الxml وتعيد لك ال root tag
def testGetRoot():
computer=get_root("books.xml")
print computer.library.books[0].name
طيب دا بالنسبة للملفات الخارجية ماذا عن النصوص الداخلية ؟ قرأت الصفحة فى متغير داخلى او ماشابه ماذا تفعل ؟
تستطيع استخدام الدالة get_root_document التى تأخذ معامل doc وهو محتوى ملف XML وتعيد لك ال root tag
ملحوظة الملفات المستخدمة فى الشرح amazon.xml و twitter.xml مأخوذه من هنا
http://railstips.org/2008/11/17/happymapper-making-xml-fun-againوبس كدا

للتحميل
http://programming-fr34ks.net/pfsoft/happymapperstable.tar.gz