Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
Just wondering if anyone was experienced with or could point me in a good direction for using XML in PHP.

I currently have an application which uses an XML config file, and I'm getting ready to sit down and plan a PHP administration site for it. After a quick glance at php.net, I'm not so certain PHP will be the right tool... the DOM functions (all 2 of them) say they're unsupported and may change, and I don't see any support for XPath in the xml pages.

Any input on this idea would be much appreciated, I'm not married to PHP if another system would be better.

-Rob
 
PHP probably isn't the most ideal language for XML, but it isn't as bad as it seems. The DOM is very weak and the only real XML parser for PHP, XPat, is astonishingly slow and clunky. On the other hand, PHP is so open ended and flexible that you can accomplish almost anything you would do using the DOM, and usually a little faster. If you're new to PHP, I have to say that XML isn't a great place to start. You should stick it out, though. Having worked in both PHP and ASP, I can honestly say that I would rather have the freedom of PHP any day.
 
Ok, not new to PHP, relatively new to XML, and pretty new to XPath... I know I could do this all by hand, but my experience with XML so far is that every time I've tried that, I've found a way that is much faster and simpler just around the corner...

So, I'll ask a little prematurely... but humor me if you will... I have an XML of this sort...

<root>
<product>
<name>Widget Man</name>
<instance id=&quot;100&quot;>
<prod_date>10/10/2010</prod_date>
<weight>12 pounds</weight>
</instance>
<instance id=&quot;101&quot;>
<prod_date>10/10/2020</prod_date>
<weight>44 pounds</weight>
</instance>
</product>
<product>
<name>Who cares!</name>
....
</root>

So I've read this into a string ($xml), then I've said

$p = xml_parser_create();

Then after that I'm a touch confused as what PHP can offer me... I've played with
xml_parse_into_struct($p, $xml, $vals, $index);

But am not having a ton of luck (seems it really hates my indented XML file, turning all my tabs and newlines into CDATA blocks)

Am I going down the right route, or am I way off?

What I want is to create an interface so that people can add/remove products and instances of products as well as the child of those instances (weight & release date in my example).

I'm also going to want to be able to modify attributes as in the id #.

Am I headed down the right route? The other app which only reads this file does some nice simple xpath queries to determine all the instances for a product whose name is &quot;Widget Man&quot; and collect those in a node list... can I do something like that here?

Thanks much.

-Rob
 
So I've added this little bit to my parsing before calling the parse into struct function... (got it from php.net)

$xml=eregi_replace(&quot;>&quot;.&quot;[[:space:]]+&quot;.&quot;<&quot;,&quot;><&quot;,$xml);

which gets rid of all the spaces and tabs and junk between my tags, but not in my actual data... seems to be working well.

Now I'll see if those arrays are more useful monday morning.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top