Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2013-11-08
4
458 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 43

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 109

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

789 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