title: Conditional Search in XML Python
body: <p>I have a xml file <code>Orders.xml</code> (excerpt follows):</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01"&gt;
    &lt;ListOrdersResult&gt;
      &lt;Orders&gt;
        &lt;Order&gt;
          &lt;LatestShipDate&gt;2015-06-02T18:29:59Z&lt;/LatestShipDate&gt;
          &lt;OrderType&gt;StandardOrder&lt;/OrderType&gt;
          &lt;PurchaseDate&gt;2015-05-31T03:58:30Z&lt;/PurchaseDate&gt;
          &lt;AmazonOrderId&gt;171-6355256-9594715&lt;/AmazonOrderId&gt;
          &lt;LastUpdateDate&gt;2015-06-01T04:18:58Z&lt;/LastUpdateDate&gt;
          &lt;ShipServiceLevel&gt;IN Std Domestic&lt;/ShipServiceLevel&gt;
          &lt;NumberOfItemsShipped&gt;0&lt;/NumberOfItemsShipped&gt;
          &lt;OrderStatus&gt;Canceled&lt;/OrderStatus&gt;
          &lt;SalesChannel&gt;Amazon.in&lt;/SalesChannel&gt;
          &lt;NumberOfItemsUnshipped&gt;0&lt;/NumberOfItemsUnshipped&gt;
          &lt;IsPremiumOrder&gt;false&lt;/IsPremiumOrder&gt;
          &lt;EarliestShipDate&gt;2015-05-31T18:30:00Z&lt;/EarliestShipDate&gt;
          &lt;MarketplaceId&gt;A21TJRUUN4KGV&lt;/MarketplaceId&gt;
          &lt;FulfillmentChannel&gt;MFN&lt;/FulfillmentChannel&gt;
          &lt;IsPrime&gt;false&lt;/IsPrime&gt;
          &lt;ShipmentServiceLevelCategory&gt;Standard&lt;/ShipmentServiceLevelCategory&gt;
        &lt;/Order&gt;
        &lt;Order&gt;
          &lt;LatestShipDate&gt;2015-06-02T18:29:59Z&lt;/LatestShipDate&gt;
          &lt;OrderType&gt;StandardOrder&lt;/OrderType&gt;
          &lt;PurchaseDate&gt;2015-05-31T04:50:07Z&lt;/PurchaseDate&gt;
          &lt;BuyerEmail&gt;dr7h1rhy6457rng@marketplace.amazon.in&lt;/BuyerEmail&gt;
          &lt;AmazonOrderId&gt;403-5551715-2566754&lt;/AmazonOrderId&gt;
          &lt;LastUpdateDate&gt;2015-06-01T07:52:49Z&lt;/LastUpdateDate&gt;
          &lt;ShipServiceLevel&gt;IN Exp Dom 2&lt;/ShipServiceLevel&gt;
          &lt;NumberOfItemsShipped&gt;2&lt;/NumberOfItemsShipped&gt;
          &lt;OrderStatus&gt;Shipped&lt;/OrderStatus&gt;
          &lt;SalesChannel&gt;Amazon.in&lt;/SalesChannel&gt;
          &lt;ShippedByAmazonTFM&gt;false&lt;/ShippedByAmazonTFM&gt;
          &lt;LatestDeliveryDate&gt;2015-06-06T18:29:59Z&lt;/LatestDeliveryDate&gt;
          &lt;NumberOfItemsUnshipped&gt;0&lt;/NumberOfItemsUnshipped&gt;
          &lt;BuyerName&gt;Ajit Nair&lt;/BuyerName&gt;
          &lt;EarliestDeliveryDate&gt;2015-06-02T18:30:00Z&lt;/EarliestDeliveryDate&gt;
          &lt;OrderTotal&gt;
            &lt;CurrencyCode&gt;INR&lt;/CurrencyCode&gt;
            &lt;Amount&gt;938.00&lt;/Amount&gt;
          &lt;/OrderTotal&gt;
          &lt;IsPremiumOrder&gt;false&lt;/IsPremiumOrder&gt;
          &lt;EarliestShipDate&gt;2015-05-31T18:30:00Z&lt;/EarliestShipDate&gt;
          &lt;MarketplaceId&gt;A21TJRUUN4KGV&lt;/MarketplaceId&gt;
          &lt;FulfillmentChannel&gt;MFN&lt;/FulfillmentChannel&gt;
          &lt;TFMShipmentStatus&gt;Delivered&lt;/TFMShipmentStatus&gt;
          &lt;PaymentMethod&gt;Other&lt;/PaymentMethod&gt;
          &lt;ShippingAddress&gt;
            &lt;StateOrRegion&gt;MAHARASHTRA&lt;/StateOrRegion&gt;
            &lt;City&gt;THANE&lt;/City&gt;
            &lt;Phone&gt;9769994355&lt;/Phone&gt;
            &lt;CountryCode&gt;IN&lt;/CountryCode&gt;
            &lt;PostalCode&gt;400709&lt;/PostalCode&gt;
            &lt;Name&gt;Ajit Nair&lt;/Name&gt;
            &lt;AddressLine1&gt;C-25 / con-7 / Chandralok CHS&lt;/AddressLine1&gt;
            &lt;AddressLine2&gt;Sector-10 ,Koper khairne&lt;/AddressLine2&gt;
          &lt;/ShippingAddress&gt;
          &lt;IsPrime&gt;false&lt;/IsPrime&gt;
          &lt;ShipmentServiceLevelCategory&gt;Expedited&lt;/ShipmentServiceLevelCategory&gt;
        &lt;/Order&gt;
      &lt;/Orders&gt;
      &lt;CreatedBefore&gt;2015-06-08T06:45:22Z&lt;/CreatedBefore&gt;
      &lt;NextToken&gt;smN7fNREdZyaJqJYLDm0ZIfVkJJPpovRb7YcCAmB0tlUojdU4H46trQzazHyYVyLqBXdLk4iogxpJASl2BeRezElfc2tdWR3lK0FtvOjoEqUrelVme04kSJ0wMvlylZkWQWPqGlbsnPaEpJjLWtrc27Vm9nDvRdgFtvOhjiqTWA16vKmtecRgbuZIF9n45mtnrZ4AbBdBTdge/hBzh1HtoVw85GaTVKBVfeXMWcfhX25HmwX5IAmwKfxnqm3JqvZ0Rjw/YZARKQMcjl5+H0CsJGesRwkZOQCBLVDshZ93sFo8v4Do3XuodaFg8ZGJDSTcawcthgh/MGM4KOIYd79q7Aq3I/8b9+STDy5JVgPyI0jQ6ftKc7EcAIwpq2cHuPbP+HgZXNbc7qI4HDvHa5YloEDUrIQbaP8qbwRHLZm6VTmGvVwLKwj6AZ0GNanrGO6&lt;/NextToken&gt;
    &lt;/ListOrdersResult&gt;
    &lt;ResponseMetadata&gt;
      &lt;RequestId&gt;f2b55344-d281-4bd3-b8b3-788be07b7656&lt;/RequestId&gt;
    &lt;/ResponseMetadata&gt;
  &lt;/ListOrdersResponse&gt;
  </code></pre>
  
  <p>I am using a python script to parse data from xml file. I want two fields from XML file <code>AmazonOrderID</code> and <code>BuyerName</code>. Some sub element in XML might not have have <code>BuyerName</code>. When I parse both individually, I get a list of 100 <code>AmazonOrder</code> and 70 <code>BuyerName</code>.</p>
  
  <p>I want to get a empty string instead of nothing. i.e. if any subelement doesn't have a buyer name, i want to include <code>''</code> instead of nothing.</p>
  
  <p>My Code: </p>
  
  <pre><code>from xml.etree import ElementTree
  
  with open('orders.xml', 'rb') as f:
      tree = ElementTree.parse(f)
  ns = {'d': 'https://mws.amazonservices.com/Orders/2013-09-01'} 
  
  for node in tree.findall('.//d:Order/d:AmazonOrderId', ns):
      oid.append(node.text)
  for node in tree.findall('.//d:Order/d:BuyerName', ns):
      bn.append(node.text)
  
  print oid
  print bn
  </code></pre>