1

Hey, Basically got a massive XML document, with lots of nodes n attributes values and all that jazz.

I'm wanting to be VERY lazy as I'm awful at XML/PHP and google seems to have failed me on this occasion.

if it possible to grab EVERY element in the XML document and make it a PHP variable?

So if my XML Doc was like maybe...

<wrap>
 <sub></sub>
  <subsub></subsub>
 <sub></sub>
  <subsub></subsub>
 <sub2></sub2>
 <sub3 value=''></sub3>
 <sub4 value=''></sub4>
</wrap>

so it would recursively check the document converting it all into php variables no matter what the name of the tags/nodes. so for example they would end up looking like maybe. $wrap; $sub1; $sub[2]; $sub2; $sub3; $sub4; $sub3['value']; $sub4['value'];

so it would have to me smart to delve deep into possibly like 8 children down?

is this possible or will i have to type out like $sub1 = $xml->wrap->sub1; because that's like...over 1k lines of code that i don't want to exist lol.

just incase the xml feed is HERE

thanks for your time :)

4
  • There's quite a few dom->array sample functions in the commnets for the SimpleXML docs on the PHP site: ca2.php.net/simplexml Commented Dec 30, 2010 at 16:59
  • You can store the whole xml file into an array variable. This array will have sub arrays and values. You can directly reference any value you want from the array after parsing. Commented Dec 30, 2010 at 17:01
  • @tHeSiD how would you go about doing that with simplexml? Commented Dec 30, 2010 at 17:05
  • I added a small piece of code in the answer, check it out. Commented Dec 30, 2010 at 19:34

3 Answers 3

1
   $xml = simplexml_load_string($response);
// $response is your xml file/output string

you can then refer to elements within the $xml object like this

$xml->children('yourschema')->children('yoururn')->Tag1->Tag2
Sign up to request clarification or add additional context in comments.

Comments

0

Your suggested approach really doesn't make sense, as you'll just end up with something that's as complex as the originating XML is and requires just as much effort to parse. As such, using the DOM parser or SimpleXML is probably a sane way to proceed.

That said, I suspect something like xpath (DOM or SimpleXML) might also help, if you're only attempting to extract certain information and aren't too concerned about the bulk of the XML.

1 Comment

im currently using simplexml ive really got no idea how to use any of the xml stuff ive never touched it before. but i need 98% of the data from the xml sheet. you say that you suggest i use simplexml, i've still no idea how to use it effectively to do what i want, ive been reading through phpnet all day and nothing i can find/do seems to solve what i want =/
0

once you load the xml data into a simpleXML element, you can access it super easily. for instance:

$xml = new SimpleXMLElement($input_xml_string);
foreach($xml->sub as $value) {
   echo $value->subsub;
}

although be careful with sub as it is a reserved word. you may have to encase it in brackets $xml->{sub}

read http://www.php.net/manual/en/simplexml.examples-basic.php

you will have to use a DOM object to do more complicated stuff like removing children.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.