Jump to content

Phasma Felis

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Phasma Felis's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I want to find all elements named "field" in an XML doc and assign a new value to each one. I'd tried something like this: foreach ($xml->xpath('//field') as &$field) $field = $value; But that doesn't work, because (I think) XPath returns a temporary array, not a reference to an actual element. ("PHP Fatal error: Cannot create references to elements of a temporary array expression") Is there a way to assign values to elements located with XPath, or do I just have to iterate over the whole document?
  2. I have a file test.xml, like this: <?xml version="1.0"?> <elements> <element name="element1">This is an element.</element> <element name="element2"> <child name="child1">A child element.</child> <child name="child2">Another child element.</child> </element> <element name="element3">This is also an element.</element> </elements> And I want to iterate over all elements, like this: <?php function iterateXml($elements, $depth) { foreach ($elements as $element) { for ($i=0; $i<$depth; $i++) echo "\t"; //indent children echo $element->getName(), " => $element\n"; iterateXml($element->children(), $depth+1); } } $xml = simplexml_load_file("test.xml"); iterateXml($xml, 0); ?> When I run that program, I get this: element => This is an element. element => child => A child element. child => Another child element. element => This is also an element. Where's the whitespace after the second element coming from? How do I get just the textual content without the whitespace?
  3. Heh, OK. One of the posting guidelines confused me: "3. Pick the right forum for your question. If you are very new to PHP or programming in general, then post to Newbie Help. People are more likely to be patient with you there. If you post to PHP Help, the assumption is that you have gained a level of competence with PHP..." Anyway, thanks for your help. Sounds like it's pretty much as I expected...sigh. I'll talk to the client tomorrow and figure out the best way to proceed.
  4. Okay, I've done a bit of PHP work before, but I'm still pretty much a newbie to this language. I'm working on a project for a client, and want to use XML to store data (mainly medical forms, blank and filled). Everything I've seen says that XML is much, much easier to handle with PHP 5 and SimpleXML. Unfortunately, the client's box is still running PHP 4.4.0-4, and they don't want to upgrade for fear of breaking the several dozen other sites running on that server. So I have two questions. First, what's the best way to handle XML parsing in PHP 4? Second, are they right to worry that upgrading will break their other sites, or should I try to talk them into it? Is it possible to use both on the same webserver? (Also, where the heck is the Newbie Help forum mentioned in the posting guidelines? I even tried a Google sitesearch, and found lots of people asking where it was, but no answers.) Thanks for your help!
×
×
  • 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.