Solved

Parsing XML with PHP. Not sure about attributes and returns...

Posted on 2013-11-08
4
451 Views
Last Modified: 2013-11-08
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:
<?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 />";
?>

Open in new window

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>

Open in new window

0
Comment
Question by:tjyoung
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39633155
To access an attribute, you use the attributes() function:

echo $options[0]->customer->contact->name->attributes()->part;

As for your line endings - you are keeping them, but because you're echoing them out in PHP they get lost in the view. If you look at the source of your page once it's run you'll see the line endings are still there. If you need to see them in HTML, wrap the comment in <pre> tags:

echo "<pre>";
echo $options[0]->customer->comments;
echo "</pre>";

Open in new window

If you don't want the PRE tags, then wrap the whole thing in the nl2br function. That will turn NewLines into BR tags:

echo nl2br($options[0]->customer->comments);

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39633177
Perfect. Thanks as always Chris.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39633184
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));
    }
}

Open in new window

Best regards, ~Ray
0
 
LVL 1

Author Comment

by:tjyoung
ID: 39633240
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!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now