Skip to main content
Tony Williams (Awana) 님이 #Salesforce Developer에 질문했습니다
HI I'm trying top traverse an XML tree to get the Items Tag list. I was using iterative but the problem is that it seems that I can only get the lst item but I don;t get them all. Can someone p0leaswe help me?  Thanks Here's the tree:

 

<Shipment id="103052074">

<ClientName>Awana</ClientName>

<OrderID>01061767</OrderID>

  <PurchaseOrder />

  <Name>SILVIA GIBSON</Name>

  <FirstName>SILVIA</FirstName>

  <LastName>GIBSON</LastName>

  <Company />

<Address1>11900 TRADITION LN NE </Address1>

  <Address2 />

  <City>ALBUQUERQUE</City>

  <State>NM</State>

  <PostalCode>87111-8287</PostalCode>

  <Country>UNITED STATES</Country>

  <Email>silviagibson@mailinator.com</Email>

  <Phone />

  <OrderTimestamp>2019-01-18T00:00:00</OrderTimestamp>

  <ReceivedTimestamp>2019-01-18T13:59:26.007</ReceivedTimestamp>

  <ShipmentStatus>SHIPPED</ShipmentStatus>

  <OrderType>Consumer</OrderType>

  <ShippedDate>2019-01-18T14:16:37.550</ShippedDate>

<ExpectedDeliveryDate>2019-01-18T00:00:00</ExpectedDeliveryDate> <DeliveredTimestamp /> <DeliveryException />

  <Warehouse id="160">

  <Name>Greenwood, IN</Name>

  <Address>1415 Collins Rd.</Address>

  <City>Greenwood</City>

  <State>IN</State>

  <PostalCode>46143</PostalCode>

  <Country>US</Country>

</Warehouse>

  <ShipMethod>Newgistics Parcel Select</ShipMethod>

  <ShipMethodCode>NGSPS</ShipMethodCode>

  <Tracking>9200000000000629213249</Tracking> <TrackingUrl>http://shipment.co/tracking/2817/9200000000000629213249</TrackingUrl>

<Weight>4.000000</Weight>

  <Postage />

  <GiftWrap>false</GiftWrap>

  <CustomFields>

  <BillingAddress1>1620 N Penny Ln</BillingAddress1>

  <BillingCity>Schaumburg</BillingCity>

  <BillingCompany>Cherry Hills Community Church</BillingCompany>

  <BillingCountry>United States</BillingCountry>

  <BillingEmail>silviagibson@mailinator.com</BillingEmail>

  <BillingFirstName>Heather</BillingFirstName>

  <BillingLastName>Oliver</BillingLastName>

  <BillingPhone>(866) 292-6227</BillingPhone>

  <BillingState>IL</BillingState>

  <BillingZip>60173</BillingZip>

  <Shipping>17.87</Shipping>

  <Total>190.52</Total>

  </CustomFields>

  <BackorderedItems />

  <Items>

  <Item id="1807577">

  <SKU>94900</SKU>

  <UPC />

  <Description>Cubbies AppleSeed Handbook Music CD NIV</Description>

  <Lot />

  <Qty>2</Qty>

  <CustomFields />

  <AssemblyItems />

  </Item>

  <Item id="1807606">

  <SKU>96543</SKU>

  <UPC />

  <Description>Cubbies HoneyComb Teaching Plans NKJV</Description>

  <Lot />

  <Qty>1</Qty>

  <CustomFields />

  <AssemblyItems />

  </Item>

  <Item id="1807670">

  <SKU>96918</SKU>

  <UPC />

  <Description>Game Pin (4)</Description>

  <Lot />

  <Qty>4</Qty>

<CustomFields />

  <AssemblyItems />

  </Item>

  <Item id="1807691">

  <SKU>46421</SKU>

  <UPC />

  <Description>Gray Blouse, Leader Size 4X</Description> <Lot />

  <Qty>3</Qty> <CustomFields />

  <AssemblyItems />

  </Item>

  </Items>

 <Packages>

  <Package id="86892375">

  <TrackingNumber>9200000000000629213249</TrackingNumber>

  <Weight>4.30507</Weight>

  <BillableWeight>4.00000</BillableWeight>

  <Height>7.00000</Height>

  <Width>4.00000</Width>

  <Depth>6.00000</Depth>

</Package>

</Shipment>

 
답변 11개
  1. 2019년 1월 19일 오후 7:19
    Hi,

    You just want the <items> <Item id="1807577"> ... </item> </items>

    There is a mistake in your xml source   <Packages>

    If you just install this class you can use a subset of XPathhttps://github.com/JenniferSimonds/apex-xpath/blob/1.0.1/XPath.cls

    https://github.com/JenniferSimonds/apex-xpath

    Dom.XmlNode[] items = xp.find('/Shipment/Items/Item');

    You just need this XPath expression : /Shipment/Items/Item   (  shortest way to get all the items )

     

    String xml = '<Shipment id="103052074"> \n' +

    ' <ClientName>Awana</ClientName> \n' +

    ' <OrderID>01061767</OrderID> \n' +

    ' <PurchaseOrder /> \n' +

    ' <Name>SILVIA GIBSON</Name> \n' +

    ' <FirstName>SILVIA</FirstName> \n' +

    ' <LastName>GIBSON</LastName> \n' +

    ' <Company /> \n' +

    ' <Address1>11900 TRADITION LN NE </Address1> \n' +

    ' <Address2 /> \n' +

    ' <City>ALBUQUERQUE</City> \n' +

    ' <State>NM</State> \n' +

    ' <PostalCode>87111-8287</PostalCode> \n' +

    ' <Country>UNITED STATES</Country> \n' +

    ' <Email>silviagibson@mailinator.com</Email> \n' +

    ' <Phone /> \n' +

    ' <OrderTimestamp>2019-01-18T00:00:00</OrderTimestamp> \n' +

    ' <ReceivedTimestamp>2019-01-18T13:59:26.007</ReceivedTimestamp> \n' +

    ' <ShipmentStatus>SHIPPED</ShipmentStatus> \n' +

    ' <OrderType>Consumer</OrderType> \n' +

    ' <ShippedDate>2019-01-18T14:16:37.550</ShippedDate> \n' +

    ' <ExpectedDeliveryDate>2019-01-18T00:00:00</ExpectedDeliveryDate> <DeliveredTimestamp /> <DeliveryException /> \n' +

    ' <Warehouse id="160"> \n' +

    ' <Name>Greenwood, IN</Name> \n' +

    ' <Address>1415 Collins Rd.</Address> \n' +

    ' <City>Greenwood</City> \n' +

    ' <State>IN</State> \n' +

    ' <PostalCode>46143</PostalCode> \n' +

    ' <Country>US</Country> \n' +

    ' </Warehouse> \n' +

    ' <ShipMethod>Newgistics Parcel Select</ShipMethod> \n' +

    ' <ShipMethodCode>NGSPS</ShipMethodCode> \n' +

    ' <Tracking>9200000000000629213249</Tracking> <TrackingUrl>http://shipment.co/tracking/2817/9200000000000629213249</TrackingUrl> \n' +

    ' <Weight>4.000000</Weight> \n' +

    ' <Postage /> \n' +

    ' <GiftWrap>false</GiftWrap> \n' +

    ' <CustomFields> \n' +

    ' <BillingAddress1>1620 N Penny Ln</BillingAddress1> \n' +

    ' <BillingCity>Schaumburg</BillingCity> \n' +

    ' <BillingCompany>Cherry Hills Community Church</BillingCompany> \n' +

    ' <BillingCountry>United States</BillingCountry> \n' +

    ' <BillingEmail>silviagibson@mailinator.com</BillingEmail> \n' +

    ' <BillingFirstName>Heather</BillingFirstName> \n' +

    ' <BillingLastName>Oliver</BillingLastName> \n' +

    ' <BillingPhone>(866) 292-6227</BillingPhone> \n' +

    ' <BillingState>IL</BillingState> \n' +

    ' <BillingZip>60173</BillingZip> \n' +

    ' <Shipping>17.87</Shipping> \n' +

    ' <Total>190.52</Total> \n' +

    ' </CustomFields> \n' +

    ' <BackorderedItems /> \n' +

    ' <Items> \n' +

    ' <Item id="1807577"> \n' +

    ' <SKU>94900</SKU> \n' +

    ' <UPC /> \n' +

    ' <Description>Cubbies AppleSeed Handbook Music CD NIV</Description> \n' +

    ' <Lot /> \n' +

    ' <Qty>2</Qty> \n' +

    ' <CustomFields /> \n' +

    ' <AssemblyItems /> \n' +

    ' </Item> \n' +

    ' <Item id="1807606"> \n' +

    ' <SKU>96543</SKU> \n' +

    ' <UPC /> \n' +

    ' <Description>Cubbies HoneyComb Teaching Plans NKJV</Description> \n' +

    ' <Lot /> \n' +

    ' <Qty>1</Qty> \n' +

    ' <CustomFields /> \n' +

    ' <AssemblyItems /> \n' +

    ' </Item> \n' +

    ' <Item id="1807670"> \n' +

    ' <SKU>96918</SKU> \n' +

    ' <UPC /> \n' +

    ' <Description>Game Pin (4)</Description> \n' +

    ' <Lot /> \n' +

    ' <Qty>4</Qty> \n' +

    ' <CustomFields /> \n' +

    ' <AssemblyItems /> \n' +

    ' </Item> \n' +

    ' <Item id="1807691"> \n' +

    ' <SKU>46421</SKU> \n' +

    ' <UPC /> \n' +

    ' <Description>Gray Blouse, Leader Size 4X</Description> <Lot /> \n' +

    ' <Qty>3</Qty> <CustomFields /> \n' +

    ' <AssemblyItems /> \n' +

    ' </Item> \n' +

    ' </Items> \n' +

    ' <Package id="86892375"> \n' +

    ' <TrackingNumber>9200000000000629213249</TrackingNumber>\n' +

    ' <Weight>4.30507</Weight> \n' +

    ' <BillableWeight>4.00000</BillableWeight> \n' +

    ' <Height>7.00000</Height> \n' +

    ' <Width>4.00000</Width> \n' +

    ' <Depth>6.00000</Depth> \n' +

    '</Package>\n' +

    '</Shipment> \n';

    XPath xp = new XPath(xml);

    Dom.XmlNode[] items = xp.find('/Shipment/Items/Item');

    for (Dom.XmlNode item : items) {

    String item_id = item.getAttribute('id',null);

    String item_weight = xp.getText(item, '', './SKU');

    String item_description = xp.getText(item, '', './Description');

    system.debug('item:' + item_id + ' = ' + item_weight + ' = ' + item_description);

    }

     
0/9000