Joe Morgan
Joe Morgan

Reputation: 1771

PHP Parsing Case Sensitivity

I'm parsing a file that is set up like so:

<blah-name name="Turkey">
<blah-city name="Test"/>
<CAPS>
  <type type="5">
  <type-two type="6">
</CAPS>

At the top of my file, I create $xmlobj, like so:

$xmlobj = simplexml_load_file("TEST.XML");

I'm then inserting the values into a table by creating a class that takes all of these as properties:

$record->{'blah-city'}->attributes()->name;

Everything blows up when I try to dig into my capitalized XML, though:

$record->{'CAPS'}->type->attributes()->type;

My server blows up on the line above with this error: Fatal error: Call to a member function attributes() on a non-object and then it lists the line of the file where the above piece of code was written.

I've tried to do a var_dump($record->{'CAPS'}); and it shows that the entire structure properly exists with values intact.

Does anyone know how I could go about correcting this issue and moving forward with my work? It would be greatly appreciated!

Upvotes: 1

Views: 1604

Answers (2)

Mr. Smith
Mr. Smith

Reputation: 5558

Have you tried doing this:

$record->CAPS->type->attributes()->type;

Just without the {'CAPS'} around it, since it's not hyphenated thats not necessary.

*Edit as per your new comment below:

Interesting result. When I tracked down {'type-two'}->attributes()->id, it displayed the proper value.

In that case, it's probably that it has a problem with type->attributes()->type. PHP might have an internal memory reference problem to type since it has the same name. Try changing

<type type="5">

to

<type-one type="5">

in your XML. This should sort out your problem. You can then access it like this:

$record->{'CAPS'}->{'type-one'}->attributes()->type;

In addition, have you tried this?

$type_attributes = $record->{'CAPS'}->{'type-one'}->attributes();
$type_value = $type_attributes->type;

That could potentially solve your problem. I also found this Stack Overflow link that may be of assistance.

Upvotes: 1

friederbluemle
friederbluemle

Reputation: 37213

The above XML code is not valid xml markup. I don't know how your complete file looks like, but assuming the following valid xml file:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <blah-name name="Turkey" />
    <blah-city name="Test" />
    <CAPS>
        <type type="5" />
        <type-two type="6" />
    </CAPS>
</data>

The following PHP Code works as expected:

$xmlobj = simplexml_load_file("TEST.XML");
$record = new stdClass();
foreach ($xmlobj as $key=>$value) {
    $record->$key = $value; 
}
var_dump($record->{'CAPS'});

Upvotes: 0

Related Questions