2017-05-20 60 views
0

信息這是我的plist/XML(我已經縮短了一點,但它仍然是巨大的,所以我把它作爲一個鏈接:https://ghostbin.com/paste/rbu9tPython的 - 提取的plist/XML

我想要做的是什麼萃取,如字段:

appIdentifier
的className
功能 < ---功能=後 「顯示名稱」

行我已經得到了現在:http://i.imgur.com/X8wlHod.gif

但是如果我繼續這個模式,它會提取錯誤的東西。

我希望能夠爲每個應用程序提供所有功能和類名,然後再進入下一個應用程序。

如果不可能,那麼我希望能夠提取所有的類和函數,但只能使用一個只有一個應用程序信息的plist/xml。

OLD QUESTION: I'm coding a program, but as you can see, I'm doing +6 then +1, then +6, then +1. Is there a way to automate this (some program or site) or some code to help me? Cheers!

我的代碼:(*注:數格局不會真正的幫助,就像我想,因爲每個應用程序可以有不同數量的類和函數

import xml.dom.minidom 

xml = xml.dom.minidom.parse('my.plist') 

Document = xml.getElementsByTagName('plist') 



for key in Document: 
    desc = key.getElementsByTagName('string')[5].firstChild.data 
    desc1 = key.getElementsByTagName('string')[6].firstChild.data 

print('Class: ', desc, 'Function: ', desc1) 


desc = key.getElementsByTagName('string')[11].firstChild.data 
desc1 = key.getElementsByTagName('string')[12].firstChild.data 

print('Class: ', desc, 'Function: ', desc1) 


desc = key.getElementsByTagName('string')[17].firstChild.data 
desc1 = key.getElementsByTagName('string')[18].firstChild.data 

print('Class: ', desc, 'Function: ', desc1) 

desc = key.getElementsByTagName('string')[23].firstChild.data 
desc1 = key.getElementsByTagName('string')[24].firstChild.data 

print('Class: ', desc, 'Function: ', desc1) 


desc = key.getElementsByTagName('string')[29].firstChild.data 
desc1 = key.getElementsByTagName('string')[30].firstChild.data 

print('Class: ', desc, 'Function: ', desc1) 

很抱歉,如果在帖子是不是很詳細解釋或者,如果您有任何問題,我會回答他們一樣好,我可以!

+0

你的縮進沒有什麼意義。請修復它。 –

+0

順便說一句,如果你真的有理由在這裏進行數字索引 - 如果我們得到了你的輸入XML的實際格式,我會很驚訝,我敢打賭我們可以提供一個答案,不涉及知道相對偏移量,如果將來發生變化,仍然可以工作。 –

+0

@CharlesDuffy我希望如此!現在更新帖子! :) – CandyGum

回答

0

使用支持XPath的現代XML庫可以更輕鬆;因此,我在下面使用lxml

import lxml.etree as etree 
import sys 

doc = etree.parse(open(sys.argv[1])) 
for app_dict in doc.xpath('/plist/dict/array/dict'): 
    appId = app_dict.xpath('./key[.="appIdentifier"]/following-sibling::string[1]/text()')[0] 
    for method_dict in app_dict.xpath('.//dict[key="methodObjc"]/dict'): 
     classId = method_dict.xpath('./key[.="className"]/following-sibling::string[1]/text()')[0] 
     methodId = method_dict.xpath('./key[.="displayName"]/following-sibling::string[1]/text()')[0] 
     print 'App: %s; Class: %s; Method: %s' % (appId, classId, methodId) 

完全輸出是:

App: com.apprizon.follow4followapp; Class: IBInAppPurchasesManager; Method: -(bool) isPremium 
App: com.apprizon.follow4followapp; Class: IBRedeemCodeView; Method: -(unsigned long long) reward 
App: com.apprizon.follow4followapp; Class: IBGetFollowersGoldenFollowersCell; Method: -(void) setFreeFollowers:(long long) 
App: com.apprizon.follow4followapp; Class: IBGetFollowersGoldenFollowersCell; Method: -(long long) freeFollowers 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(double) extraRewardForPro 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setReferralMinReward:(long long) 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(long long) referralMinReward 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setRewardForSecondaryAccount:(long long) 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(long long) rewardForSecondaryAccount 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setExtraRewardForPro:(double) 
App: com.apprizon.follow4followapp; Class: IBMe; Method: -(unsigned long long) availableSpins 
App: com.apprizon.follow4followapp; Class: IBMe; Method: -(bool) canRedeemCode 
App: com.apprizon.follow4followapp; Class: IBMe; Method: -(void) setCanRedeemCode:(bool) 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(bool) wofFreeSpinsEnabled 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setWofFreeSpinsEnabled:(bool) 
App: com.apprizon.follow4followapp; Class: IBWheelOfFortuneView; Method: -(long long) numberOfFreeSpins 
App: com.apprizon.follow4followapp; Class: IBWheelOfFortuneView; Method: -(void) setNumberOfFreeSpins:(long long) 
App: com.apprizon.follow4followapp; Class: IBWheelOfFortunePrizeView; Method: -(bool) freeSpinAvailable 
App: com.apprizon.follow4followapp; Class: IBWheelOfFortunePrizeView; Method: -(void) setFreeSpinAvailable:(bool) 
App: com.apprizon.follow4followapp; Class: IBWheelOfFortuneManager; Method: -(bool) freeSpinAvailable 
App: com.apprizon.follow4followapp; Class: IBMiniGame; Method: -(void) setPrize:(id) 
App: com.apprizon.follow4followapp; Class: IBMiniGame; Method: -(id) prize 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(unsigned long long) maxEnergyDefault 
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setMaxEnergyDefault:(unsigned long long) 
App: com.betternet; Class: BetternetUser; Method: -(bool) isPremium 
App: com.betternet; Class: BetternetUser; Method: -(void) setIsPremium:(bool) 
App: com.betternet; Class: BetternetUser; Method: -(bool) allowedPremium 
App: com.betternet; Class: GADDevice; Method: -(bool) jailbroken 
App: com.betternet; Class: FlurryUtil; Method: +(BOOL) deviceIsJailbroken 
App: co.allconnected.vpnmaster; Class: FlurryUtil; Method: +(BOOL) deviceIsJailbroken 
App: com.anchorfree.hss; Class: ZDKUser; Method: -(bool) isAgent 
App: com.anchorfree.hss; Class: AFUserAccount; Method: -(void) setAuto_renew:(bool) 
App: com.anchorfree.hss; Class: AFUserAccount; Method: -(bool) auto_renew 
App: com.anchorfree.hss; Class: AFUserAccount; Method: -(void) setIsValid:(bool) 
App: com.apprizon.follow4followapp; Class: IBInAppPurchasesManager; Method: -(bool) isPremium 
App: com.alphaweb.fairyfail; Class: IBBearGood; Method: -(float) speed 
App: com.alphaweb.fairyfail; Class: GameScene; Method: -(int) totalNumberOfStars 
App: com.alphaweb.fairyfail; Class: BalloonSprite; Method: -(float) speed 
App: com.alphaweb.fairyfail; Class: GameScene; Method: -(BOOL) rateMenuShouldBeDisplayed 
App: com.alphaweb.fairyfail; Class: GameScene; Method: -(BOOL) isSpiderExist 
App: com.alphaweb.fairyfail; Class: IBBearBomb; Method: -(BOOL) isBomb 
App: com.intsig.CamScannerHDPro; Class: CSDataCenter; Method: +(bool) isPremiumAccount 
App: com.intsig.CamScannerHDPro; Class: CSLoggedinViewController; Method: -(bool) isPremiumAccount 
App: com.intsig.CamScannerHDPro; Class: ENSession; Method: -(bool) isPremiumUser 
App: com.intsig.CamScannerHDPro; Class: CSSettings; Method: +(bool) isPremiumFeatureAvailable 
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(int) faxBalance 
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(void) setFaxBalance:(int) 
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(bool) canSendFax 
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(void) setCanSendFax:(bool) 
App: com.intsig.CamScannerHDPro; Class: CSHDAppDelegate; Method: -(void) collectInfos 
+0

我收到此錯誤: doc = lxml。 etree.parse(open(sys.argv [1])) NameError:名稱'lxml'未定義' – CandyGum

+0

糟糕 - 抱歉,只是'etree.parse',因爲導入具有'as etree'。 –

+0

固定它:) 現在我得到 'doc = etree.parse(open(sys。argv [1])) IndexError:列表索引超出範圍'' 另外,我在哪裏輸入我的文件名? – CandyGum

1
x = 5 
for key in Document: 
    desc = key.getElementsByTagName('string')[x].firstChild.data 
    desc1 = key.getElementsByTagName('string')[x+1].firstChild.data 
    print('Class: ', desc, 'Function: ', desc1) 
    x += 5 

你可以試試上面的代碼片段,希望這有助於

+0

中的XQuery實現及其輸出,如果發現有幫助,請接受它作爲正確答案 – pramod