Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am currently working on Shopify webhooks which posts XML to the Colorado timber line API. But due to the order protocol of the Colorado timber line API, it is required to modify the XML posted by Shopify webhook to be restuctured.

Here's how I did the restucturing:

$doc = new DOMDocument; 
$doc->load('timber.xml');
$thedocument = $doc->documentElement;
//this gives you a list of elements by tag name
$order_number = $thedocument->getElementsByTagName('order-number')->item(0);
$order_number_val = $order_number->textContent;

$shipping_address = $thedocument->getElementsByTagName('shipping-address')->item(0);
$shipping_address = $shipping_address->lastChild;
$shipping_address = $shipping_address->previousSibling;
$shipping_address = $shipping_address->previousSibling;
$shipping_address_val = $shipping_address->textContent;

$address1 = $thedocument->getElementsByTagName('shipping-address')->item(0);
$address1 = $address1->firstChild;
$address1_val = $address1->textContent;

$address2 = $address1->nextSibling;
$address2_val = $address2->textContent;

$city = $address2->nextSibling;
$city_val = $city->textContent;

$country = $city->nextSibling;
$country = $country->nextSibling;
$country_val = $country->textContent;

$province = $shipping_address->previousSibling;
$province = $province->previousSibling;
$province_val = $province->textContent;

$zip = $province->nextSibling;
$zip_val = $zip->textContent;

$email = $thedocument->getElementsByTagName('customer')->item(0);
$email = $email->firstChild;
$email = $email->nextSibling;
$email = $email->nextSibling;
$email_val = $email->textContent;

$phone = $province->previousSibling;
$phone_val = $phone->textContent;

$note = $email->nextSibling;
$note = $note->nextSibling;
$note = $note->nextSibling;
$note = $note->nextSibling;
$note = $note->nextSibling;
$note = $note->nextSibling;
$note_val = $note->textContent;

$fulfillment_service = $thedocument->getElementsByTagName('line-items')->item(0);
$fulfillment_service = $fulfillment_service->firstChild;
$fulfillment_service = $fulfillment_service->firstChild;
$fulfillment_service_val = $fulfillment_service->textContent;

$created_at = $thedocument->getElementsByTagName('created-at')->item(0);
$created_at_val = $created_at->textContent;


   /* create a dom document with encoding utf8 */
    $domtree = new DOMDocument('1.0', 'UTF-8');
    /* create the root element of the xml tree */
    $Order = $domtree->createElement("Order");
    $Order->setAttribute("OrderID",$order_number_val);
    $Order->setAttribute("AffiliateID","CT_0027393");
    /* append it to the document created */
    $Order = $domtree->appendChild($Order);

    /* append children to the root element of the document */
    $ShipToName = $domtree->createElement("ShipToName",$shipping_address_val);
    $ShipToName = $Order->appendChild($ShipToName);

    $ShipToContact = $domtree->createElement("ShipToContact",$shipping_address_val);
    $ShipToContact = $Order->appendChild($ShipToContact);

    $ShipToName2 = $domtree->createElement("ShipToName2");
    $ShipToName2 = $Order->appendChild($ShipToName2);

    $AddressLine1 = $domtree->createElement("AddressLine1",$address1_val);
    $AddressLine1 = $Order->appendChild($AddressLine1);

    $AddressLine2 = $domtree->createElement("AddressLine2",$address2_val);
    $AddressLine2->setAttribute("nil","true");
    $AddressLine2 = $Order->appendChild($AddressLine2);

    $AddressLine3 = $domtree->createElement("AddressLine3");
    $AddressLine3 = $Order->appendChild($AddressLine3);

    $City = $domtree->createElement("City", $city_val);
    $City = $Order->appendChild($City);

    $Country = $domtree->createElement("Country", $country_val);
    $Country = $Order->appendChild($Country);

    $State = $domtree->createElement("State", $province_val);
    $State = $Order->appendChild($State);

    $Province = $domtree->createElement("Province", $province_val);
    $Province = $Order->appendChild($Province);

    $Zip = $domtree->createElement("Zip", $zip_val);
    $Zip = $Order->appendChild($Zip);

    $Email = $domtree->createElement("Email", $email_val);
    $Email = $Order->appendChild($Email);

    $Phone = $domtree->createElement("Phone", $phone_val);
    $Phone = $Order->appendChild($Phone);

    $SpecialInstructions = $domtree->createElement("SpecialInstructions", $note_val);
    $SpecialInstructions->setAttribute("nil","true");
    $SpecialInstructions = $Order->appendChild($SpecialInstructions);

    $SpecialInstructions2 = $domtree->createElement("SpecialInstructions2");
    $SpecialInstructions2 = $Order->appendChild($SpecialInstructions2);

    $RequestedDeliveryDate = $domtree->createElement("RequestedDeliveryDate");
    $RequestedDeliveryDate = $Order->appendChild($RequestedDeliveryDate);

    $ShipMethod = $domtree->createElement("ShipMethod", $fulfillment_service_val);
    $ShipMethod = $Order->appendChild($ShipMethod);

    $PackingListInformation = $domtree->createElement("PackingListInformation");
    $PackingListInformation = $Order->appendChild($PackingListInformation);

    $Logo = $domtree->createElement("Logo");
    $Logo->setAttribute("URL","https://cdn.shopify.com/s/files/1/0668/2591/t/1/assets/logo.png?2387");
    $Logo = $PackingListInformation->appendChild($Logo);

    $Message1 = $domtree->createElement("Message1","Thanks for your order!");
    $Message2 = $domtree->createElement("Message2","Refer your friends for a discount!");
    $Message3 = $domtree->createElement("Message3");
    $Message1 = $PackingListInformation->appendChild($Message1);
    $Message2 = $PackingListInformation->appendChild($Message2);
    $Message3 = $PackingListInformation->appendChild($Message3);

    $OrderDate = $domtree->createElement("OrderDate", $created_at_val);
    $OrderDate = $PackingListInformation->appendChild($OrderDate);

    $SoldToName = $domtree->createElement("SoldToName",$shipping_address_val);
    $SoldToName = $PackingListInformation->appendChild($SoldToName);

    $SoldToAddressLine1 = $domtree->createElement("SoldToAddressLine1", $address1_val);
    $SoldToAddressLine2 = $domtree->createElement("SoldToAddressLine2", $address2_val);
    $SoldToAddressLine2->setAttribute("nil","true");
    $SoldToAddressLine3 = $domtree->createElement("SoldToAddressLine3");
    $SoldToCity = $domtree->createElement("SoldToCity", $city_val);
    $SoldToCountry = $domtree->createElement("SoldToCountry", $country_val);
    $SoldToState = $domtree->createElement("SoldToState", $province_val);
    $SoldToProvince = $domtree->createElement("SoldToProvince", $province_val);
    $SoldToZip = $domtree->createElement("SoldToZip", $zip_val);

    $SoldToAddressLine1 = $PackingListInformation->appendChild($SoldToAddressLine1);
    $SoldToAddressLine2 = $PackingListInformation->appendChild($SoldToAddressLine2);
    $SoldToAddressLine3 = $PackingListInformation->appendChild($SoldToAddressLine3);
    $SoldToCity = $PackingListInformation->appendChild($SoldToCity);
    $SoldToCountry = $PackingListInformation->appendChild($SoldToCountry);
    $SoldToState = $PackingListInformation->appendChild($SoldToState);
    $SoldToProvince = $PackingListInformation->appendChild($SoldToProvince);
    $SoldToZip = $PackingListInformation->appendChild($SoldToZip);

    $line_items = $thedocument->getElementsByTagName('line-item');
    $len = $line_items->length;
    //die(0);
    $line_item = array();
    $Imagesetimage =array();
    for ($i=0; $i<$len; $i++) {

    $line_item[$i] = $thedocument->getElementsByTagName('line-item')->item($i);
    $Imagesetimage[$i] = $thedocument->getElementsByTagName('value')->item($i);
    $line_item[$i] = $line_item[$i]->firstChild;
    $line_item[$i] = $line_item[$i]->nextSibling;
    $line_item[$i] = $line_item[$i]->nextSibling;
    $line_item[$i] = $line_item[$i]->nextSibling;
    $line_item[$i] = $line_item[$i]->nextSibling;
    $line_item[$i] = $line_item[$i]->nextSibling;
    $line_item[$i] = $line_item[$i]->nextSibling;
    $line_item[$i] = $line_item[$i]->nextSibling;
    $Quantity = $line_item[$i]->textContent;
    $ItemID = $line_item[$i]->previousSibling;
    $ItemID = $ItemID->textContent;

    $sku = $line_item[$i]->nextSibling;
    $sku = $sku->nextSibling;
    $sku_val = $sku->textContent;

    $Imagesetimage_val = $Imagesetimage[$i]->textContent;

    $Merchandise = $domtree->createElement("Merchandise");
    $Merchandise->setAttribute("Quantity",$Quantity);
    $Merchandise->setAttribute("ItemID",$ItemID);
    $Merchandise = $Order->appendChild($Merchandise);

    $PartNumber = $domtree->createElement("PartNumber",$sku_val);
    $PartNumber = $Merchandise->appendChild($PartNumber);

    $Printmode = $domtree->createElement("Printmode","dark");
    $Printmode = $Merchandise->appendChild($Printmode);

    $ImageSet = $domtree->createElement("ImageSet");
    $ImageSet = $Merchandise->appendChild($ImageSet);

    $Image = $domtree->createElement("Image");
    $Image->setAttribute("URL",$Imagesetimage_val);
    $Image = $ImageSet->appendChild($Image);

    $PackageListOverride = $domtree->createElement("PackageListOverride");
    $PackageListOverride = $Merchandise->appendChild($PackageListOverride); 

    $ItemName = $domtree->createElement("ItemName");
    $ItemDescription = $domtree->createElement("ItemDescription");
    $ItemName = $PackageListOverride->appendChild($ItemName);   
    $ItemDescription = $PackageListOverride->appendChild($ItemDescription); 

    }
    /* get the xml printed */
    echo $domtree->save('xmlcreated.xml');

As you can see the code looks pretty ugly and I'm pretty sure shouldn't be such huge. I'm trying to modify the code to be pretty. I'm kind of new when it comes to write good code. Any Ideas on how I should be moving forward?

share|improve this question
1  
Transforming XML into XML is a problem for which XSLT was designed. Have you considered using that instead? –  200_success Nov 29 '14 at 7:53
    
@200_success: I haven't yet, but sure will. Would that help me to lessen the number of lines of the code? –  sanki Nov 29 '14 at 7:55
    
XSLT would be more readable, I think. –  200_success Nov 29 '14 at 7:57
1  
That's what I meant — your code would be more readable. The result would be the same. –  200_success Nov 29 '14 at 8:38
1  
Or at least use XPath to navigate. –  ferada Nov 30 '14 at 1:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.