views:

149

answers:

2

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!

+1  A: 

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.

Mr. Smith
Yeah. It still gives me the same error.
Joe Morgan
Can you tell me the result of $record->{'CAPS'}->{'type-two'}->attributes()->type; please?
Mr. Smith
Interesting result. When I tracked down {'type-two'}->attributes()->id, it displayed the proper value.
Joe Morgan
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.
Mr. Smith
Well, I would like to, but this is an external XML file that we are fed from an outside company, so I'm unable to alter the XML, unless there's some way to do it on the fly, in-line or something.
Joe Morgan
A: 

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'});
friederbluemle
As you can see, he is able to access some parts, and the var_dump shows the elements he needs perfectly fine.
Mr. Smith
Yeah - I'm able to get down to everything, so long as it is outside of the <CAPS></CAPS> XML
Joe Morgan
Is it throwing an error on every upper case tag? What happens if you change it to say <TEST></TEST> and try access it?
friederbluemle
There are a few other instances where the uppercase tags seem to work fine now. I'm no longer convinced that it's the uppercase tag. My best guess is that it has something to do with what Mr. Smith mentioned in the comments of his answer, but I'm not sure.
Joe Morgan