简体   繁体   中英

Python - Extract from .plist

I got this piece of code, a .plist. Is there a way for me to extract certain sections of it? I only want the strings, and if possible, just some certain strings.

Like if I could extract -(bool) isAgent Knowing <key>displayName</key> is above it, and <key>prefix</key> under it.

                <string>ZDKUser</string>
                <key>displayName</key>
                <string>-(bool) isAgent</string>
                <key>prefix</key>

Out of this block ^

Is this possible? How would I do it? If not possible with python, how else?

    <array>
        <dict>
            <key>UUID</key>
            <string>123-456-789-0123</string>
            <key>purchaseNumber</key>
            <string>y.number.x</string>
            <key>purchaseID</key>
            <string>3.5.6</string>
            <key>name</key>
            <string>*Product Name*</string>
            <key>purchased</key>
            <false/>
            <key>units</key>
            <array>
                <dict>
                    <key>methodObjc</key>
                    <dict>
                        <key>className</key>
                        <string>ZDKUser</string>
                        <key>displayName</key>
                        <string>-(bool) isAgent</string>
                        <key>prefix</key>
                        <string>-</string>
                        <key>selector</key>
                        <string>isAgent</string>
                        <key>typeEncoding</key>
                        <string>B16@0:8</string>
                    </dict>
                    <key>name</key>
                    <string>Unit for -(bool) isAgent</string>
                    <key>overrides</key>
                    <array>
                        <dict>
                            <key>argument</key>
                            <integer>0</integer>
                            <key>type</key>
                            <dict>
                                <key>subtype</key>
                                <integer>0</integer>
                                <key>type</key>
                                <integer>6</integer>
                            </dict>
                            <key>value</key>
                            <dict>
                                <key>type</key>
                                <integer>6</integer>
                                <key>value</key>
                                <true/>
                            </dict>
                        </dict>
                    </array>
                </dict>
                <dict>
                    <key>methodObjc</key>
                    <dict>
                        <key>className</key>
                        <string>AFUserAccount</string>
                        <key>displayName</key>
                        <string>-(void) setAuto_renew:(bool)</string>
                        <key>prefix</key>
                        <string>-</string>
                        <key>selector</key>
                        <string>setAuto_renew:</string>
                        <key>typeEncoding</key>
                        <string>v20@0:8B16</string>
                    </dict>
                    <key>name</key>
                    <string>Unit for -(void) setAuto_renew:(bool)</string>
                    <key>overrides</key>
                    <array>
                        <dict>
                            <key>argument</key>
                            <integer>1</integer>
                            <key>type</key>
                            <dict>
                                <key>subtype</key>
                                <integer>0</integer>
                                <key>type</key>
                                <integer>6</integer>
                            </dict>
                            <key>value</key>
                            <dict>
                                <key>type</key>
                                <integer>6</integer>
                                <key>value</key>
                                <true/>
                            </dict>
                        </dict>
                    </array>
                </dict>
<array/>
</dict>
</plist>
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)

Answer by @Charles Duffy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM