tjyoung
asked on
Parsing XML with PHP. Not sure about attributes and returns...
Hi,
Trying to figure out how to echo out the below xml. I'm having particular trouble with the 'name'. Not sure how to handle the part= attributes? Also the code below is what I'm using currently just to try and spit out parts. I lose my returns in the comments section. Is there a way I should be doing it to hold on to that so its not a giant paragraph? My end goal is to import into mysql db, but this is a start...
Code sample I'm using:
Trying to figure out how to echo out the below xml. I'm having particular trouble with the 'name'. Not sure how to handle the part= attributes? Also the code below is what I'm using currently just to try and spit out parts. I lose my returns in the comments section. Is there a way I should be doing it to hold on to that so its not a giant paragraph? My end goal is to import into mysql db, but this is a start...
Code sample I'm using:
<?php
$prospect = simplexml_load_file('sample.xml');
// get all song elements as an array
$options = iterator_to_array($prospect->prospect, false);
// output json
echo $options[0]->customer->contact->name."<br />";
echo $options[0]->customer->contact->email."<br />";
echo $options[0]->customer->contact->phone."<br />";
echo $options[0]->vehicle->year."<br />";
echo $options[0]->customer->comments."<br />";
?>
XML sample below:<?xml version="1.0"?>
<?adf version="1.0"?>
<adf>
<prospect>
<requestdate>2013-11-07CST21:25:52-06:00</requestdate>
<vehicle interest="buy" status="used">
<year></year>
<make></make>
<model></model>
<vin></vin>
<stock></stock>
<trim></trim>
</vehicle>
<customer>
<contact>
<name part="first">Jessica</name>
<name part="last">Smith</name>
<email>Jsmith@live.ca</email>
<phone>506-402-1215</phone>
<address>
<street line="1"></street>
<city>Moncton</city>
<regioncode>NB</regioncode>
<postalcode></postalcode>
<country>CA</country>
</address>
</contact>
<comments>
<![CDATA[Can't read full transcript? Visit http://www.mydomain.com/admin/viewtrans.php?ref=bedff7434c1576262a98df46b9c128ae to view it
Customer is looking for a used 4 door sedan type of vehicle that will keep her within $350 for monthly payment.
Amy: May I help you find what you're looking for?
Visitor: Wondering about financing
Amy: My name is Amy. May I please have your name so I know who I'm chatting with?
Jess: Jessica
Amy: Hello, Jessica. Nice to meet you! I'd be happy to help. May I know what particular vehicle are you interested in?
Jess: I would like a sedan 4 door used
Amy: Great. Are you trying to stay within a certain price range?
Jess: I have an insurance payout of $2,750 and hoping to put that on a down payment for a car that would have a max everything included monthly payment of $350
Jess: Would there be anything like that available?
Amy: Sounds good. We may have something that may work for you. I'd be glad to verify the inventory from my Internet Manager and get back to you with what we have that may interest you including the specifics so you'll have options. May I have your email address where we can forward the details to?
Jess: Jsmith@live.ca
Amy: I appreciate that, Jessica. May I also have a phone number just in case we have immediate questions?
Jess: 506-555-1234
Amy: Thank you for the information. Before I forget, may I have the correct spelling of your last name so we can address you properly?
Jess: Is it possible to have two people on one finance?
Jess: Smith - its easier to type than it is to say
Amy: Do you mean a co-signer for you?
Jess: My common law partner and I would like to have joint ownership
Jess: Saturday 12:30-2:30 I unfortunately have a very tight schedule
Amy: Fantastic. Looking forward to see you at the dealership. I'll be forwarding this chat to my Manager as soon as we finish and rest assured that we'll be in touch. Would there be anything else I may help you today?
Jess: Nope thank you.
Amy: You're welcome. It's been nice chatting with you. If you have any further questions, please don't hesitate to chat with us again. Have a great evening, Jessica.
Jess: Thank you you as well.
]]>
</comments>
</customer>
<vendor>
<id sequence="1" source="the chat"></id>
<vendorname>My Dealership</vendorname>
<contact>
<name part="full">Internet Department</name>
<email></email>
<phone></phone>
<address>
<street line="1">1234 Somewhere Court</street>
<city>Moncton</city>
<regioncode>NB</regioncode>
<postalcode>b2w0k3</postalcode>
<country>Canada</country>
</address>
</contact>
</vendor>
<provider>
<name part="full">My Chat co</name>
<service>Chatting</service>
</provider>
<leadtype>sales</leadtype>
</prospect>
</adf>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Have a look at this. The comments contain newline characters and can be stored in the data base "as-is" then rendered with nl2br() to reconstruct the line-by-line structure.
<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);
echo '<pre>';
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28288880.html
// TEST DATA FROM THE POST AT EE
$xml = <<<EOD
<?xml version="1.0"?>
<?adf version="1.0"?>
<adf>
<prospect>
<requestdate>2013-11-07CST21:25:52-06:00</requestdate>
<vehicle interest="buy" status="used">
<year></year>
<make></make>
<model></model>
<vin></vin>
<stock></stock>
<trim></trim>
</vehicle>
<customer>
<contact>
<name part="first">Jessica</name>
<name part="last">Smith</name>
<email>Jsmith@live.ca</email>
<phone>506-402-1215</phone>
<address>
<street line="1"></street>
<city>Moncton</city>
<regioncode>NB</regioncode>
<postalcode></postalcode>
<country>CA</country>
</address>
</contact>
<comments>
<![CDATA[Can't read full transcript? Visit http://www.mydomain.com/admin/viewtrans.php?ref=bedff7434c1576262a98df46b9c128ae to view it
Customer is looking for a used 4 door sedan type of vehicle that will keep her within $350 for monthly payment.
Amy: May I help you find what you're looking for?
Visitor: Wondering about financing
Amy: My name is Amy. May I please have your name so I know who I'm chatting with?
Jess: Jessica
Amy: Hello, Jessica. Nice to meet you! I'd be happy to help. May I know what particular vehicle are you interested in?
Jess: I would like a sedan 4 door used
Amy: Great. Are you trying to stay within a certain price range?
Jess: I have an insurance payout of $2,750 and hoping to put that on a down payment for a car that would have a max everything included monthly payment of $350
Jess: Would there be anything like that available?
Amy: Sounds good. We may have something that may work for you. I'd be glad to verify the inventory from my Internet Manager and get back to you with what we have that may interest you including the specifics so you'll have options. May I have your email address where we can forward the details to?
Jess: Jsmith@live.ca
Amy: I appreciate that, Jessica. May I also have a phone number just in case we have immediate questions?
Jess: 506-555-1234
Amy: Thank you for the information. Before I forget, may I have the correct spelling of your last name so we can address you properly?
Jess: Is it possible to have two people on one finance?
Jess: Smith - its easier to type than it is to say
Amy: Do you mean a co-signer for you?
Jess: My common law partner and I would like to have joint ownership
Jess: Saturday 12:30-2:30 I unfortunately have a very tight schedule
Amy: Fantastic. Looking forward to see you at the dealership. I'll be forwarding this chat to my Manager as soon as we finish and rest assured that we'll be in touch. Would there be anything else I may help you today?
Jess: Nope thank you.
Amy: You're welcome. It's been nice chatting with you. If you have any further questions, please don't hesitate to chat with us again. Have a great evening, Jessica.
Jess: Thank you you as well.
]]>
</comments>
</customer>
<vendor>
<id sequence="1" source="the chat"></id>
<vendorname>My Dealership</vendorname>
<contact>
<name part="full">Internet Department</name>
<email></email>
<phone></phone>
<address>
<street line="1">1234 Somewhere Court</street>
<city>Moncton</city>
<regioncode>NB</regioncode>
<postalcode>b2w0k3</postalcode>
<country>Canada</country>
</address>
</contact>
</vendor>
<provider>
<name part="full">My Chat co</name>
<service>Chatting</service>
</provider>
<leadtype>sales</leadtype>
</prospect>
</adf>
EOD;
// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);
// USE AN ITERATOR TO ACCESS PROPERTIES OF THE OBJECT
foreach ($obj->prospect->vehicle as $v)
{
// GET ATTRIBUTES
$i = (string)$v['interest'];
$s = (string)$v['status'];
echo PHP_EOL . "interest: $i, status: $s";
}
// USE A NESTED ITERATOR TO ACCESS PROPERTIES OF THE OBJECT
foreach ($obj->prospect->customer as $c)
{
// COLLECT THE NAME PARTS HERE
$nom = NULL;
// THE NAME IS AN ARRAY
foreach ($c->contact->name as $n)
{
$nom .= ' ' . $n;
}
$nom = trim($nom);
echo PHP_EOL . "name: $nom";
// ACCESS THE COMMENTS
foreach ($c->comments as $x)
{
echo PHP_EOL . nl2br(trim($x));
}
}
Best regards, ~Ray
ASKER
Hi Ray, amazing... I'm going to have to stare at this for awhile to get it in my head no doubt. Thank you for putting in the note!
ASKER