Jump to content

Space in xml nodes


graham23s

Recommended Posts

Hi Guys,

 

In an xml file i am pulling from a site there is spaces between some of the nodes for example: category name has a space in the middle, does this matter at all, or is there a special way to deal with them?

 

test code:

 

<?php

$object = simplexml_load_file("data.xml");

$xmlobj = new SimpleXMLElement($object);

echo $xmlobj->root->category name;

?>

 

thanks guys

 

Graham

Link to comment
Share on other sites

Hi Mate,

 

The most akward one is this one:

 

<?xml version="1.0"?>
<root>
<bonus>
  <bonus bonus_id="5" qty="4" title="title1" variant="6"/><bonus bonus_id="6" qty="2" title="title2"/></bonus>
<category name="Stop Smoking"><product bestseller_flag="0" brand="brandname" brand_flag="0" container="tablet" dosages="150mg" generic="genericTitle" name="productName">
<description>description goes here<description>

 

i really just need the category name and the description  values.

 

cheers mate

 

Graham

 

Graham

Link to comment
Share on other sites

You need to do more reading on XML to understand what you're doing; w3schools has some fine tutorials.

 

<category name="Stop Smoking">

 

The part in bold is an element.  The element is named category.

 

The part in italics is an attribute.  The attribute's name is name and its value is Stop Smoking.

 

You'll have to double check the SimpleXML documentation on how to access attributes, but I believe you do it as if they were array elements.

 

The following may work:

<?php
$object = simplexml_load_file("data.xml");
//$xmlobj = new SimpleXMLElement($object); // <-- PRETTY SURE THIS IS REDUNDANT AND UNNECESSARY
echo $object->root->category['name'];
?>

Link to comment
Share on other sites

Hi Roopurt,

 

Thanks a lot mate, i managed to get access to the data i wanted :) and i'm a little wiser on xml, the only other part is how would i go about accessing the data on all the products, i know i need a foreach like:

 

<?php

//$xmlFile = "data.xml";

//$xmlobj = new SimpleXmlElement(file_get_contents($xmlFile));

//print_r($xmlobj);

$object = simplexml_load_file("data.xml");

foreach($object as $prodName)
{

//$xmlobj = new SimpleXMLElement($object); // <-- PRETTY SURE THIS IS REDUNDANT AND UNNECESSARY
//echo $object->category['name'];
echo "Product Name: " . $object->category->product['name'];
echo "<br />";
//echo $object->category->product->description;

//echo $prodName;

}

?>

 

any pointer son the foreach would be great, thanks for your help mate

 

Graham

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.