1

I have a plist i have parsed with lxml. Thanks to help from users here I have a full set of items i want to add, in the correct format, and can add them in. The trouble I have now is using 2 different levels of the xml file to select where to put it. If I have the following how can i be sure to insert my text over "WUT" in the set that uses both "Notes" and "13", and not in "Books" and "13"? Or the other way around?

<root>
  <key>Title</key>
  <dict>
    <key>Set</key>
    <dict>
      <key>Notes</key>
      <dict>
        <key>Tester</key>
        <array>
          <dict>
            <key>13</key>
            <dict>
              <key>Param</key>
              <array>
                <string>WUT</string>
              </array>
            </dict>
            <key>18</key>
            <dict>
              <key>Param</key>
              <array>
                <string>WUT</string>
              </array>
            </dict>
          </dict>
        </array>
      </dict>
    </dict>
    <dict>
      <key>Books</key>
      <dict>
        <key>Tester</key>
        <array>
          <dict>
            <key>13</key>
            <dict>
              <key>Param</key>
              <array>
                <string>WUT</string>
              </array>
            </dict>
          </dict>
        </array>
      </dict>
    </dict>
  </dict>
</root>

So ideally my new plist would look like:

<root>
  <key>Title</key>
  <dict>
    <key>Set</key>
    <dict>
      <key>Notes</key>
      <dict>
        <key>Tester</key>
        <array>
          <dict>
            <key>13</key>
            <dict>
              <key>Param</key>
              <array>
                <string>1</string>
                <string>2</string>
                <string>3</string>
                <string>4</string>
                <string>5</string>
              </array>
            </dict>
            <key>18</key>
            <dict>
              <key>Param</key>
              <array>
                <string>WUT</string>
              </array>
            </dict>
          </dict>
        </array>
      </dict>
    </dict>
    <dict>
      <key>Books</key>
      <dict>
        <key>Tester</key>
        <array>
          <dict>
            <key>13</key>
            <dict>
              <key>Param</key>
              <array>
                <string>WUT</string>
              </array>
            </dict>
          </dict>
        </array>
      </dict>
    </dict>
  </dict>
</root>

I have tried using xpath and running.

for plist_title in tree.xpath('//dict[key="Notes"][1]')
    for plist_tester in plist_title.xpath('//dict[key="13"][1]')
        plist_tester.insert(1,myData)

but the data only gets inserted after the last "13" and not the one I would like to define it to.

Dippy
  • 143
  • 10
  • Cannot reproduce. Please post a [mcve]. My attempt added around your code snippet put the data in the correct location. – Mark Tolonen Sep 29 '17 at 16:59

1 Answers1

1

There is a good library for parsing XMLs in python called BeautifulSoup. Check the following links out.

I am sure this is not the answer you wanted, but sometimes it's better to teach a man to fish.

Tyler R
  • 474
  • 2
  • 6
  • 15