okay, i've never delved into XML because i've never had a reason to, but i thought this might be a good place to startlet's say you have a list of links that are listed alphabetically (we'll use 10 for simplification purposes), but you have 3 different categories...when someone first clicks to load the links page, they see all 10 together, but then they can choose one of the three categories that will display only those links that belong (and multiple links can belong in multiple categories)...with me so far? here's what we have:LINKS.XML
<links> <link section="one"><a href="#">link01</a></link> <link section="one two"><a href="#">link02</a></link> <link section="one two three"><a href="#">link03</a></link> <link section="one three"><a href="#">link04</a></link> <link section="two"><a href="#">link05</a></link> <link section="two three"><a href="#">link06</a></link> <link section="three"><a href="#">link07</a></link> <subsection><subhead>subsection</subhead> <link section="one"><a href="#">link08</a></link> <link section="one two"><a href="#">link09</a></link> <link section="two three"><a href="#">link10</a></link> </subsection></links>
link01link02link03link04subsection link08 link09
5/6/2008 12:42:04 PM
If you are using PHP5 (I would very much recommend it), take a look at the DOM extension. You might do something like this:
$section = 'one'; $doc = new DomDocument(); if(!$doc->load('LINKS.XML')) { die('failed to load xml file'); } foreach($doc->getElementsByTagName('link') as $element) { if(!$element->hasAttribute('section') || strpos($element->getAttribute('section'), $section) === false) { $doc->removeChild($element); } } $filtered_doc = $doc->saveXML();
5/6/2008 1:01:33 PM
i keep getting:
Fatal error: Call to undefined method DOMDocument::getElementByTagName()
5/6/2008 3:35:07 PM
plural ElementsgetElementByTagName is JavaScript
5/6/2008 3:39:23 PM
i think it's plural in javascript too, eh?
5/6/2008 3:58:34 PM
or in .NET land, how about XMLDocument class
5/6/2008 4:07:50 PM
well, damnation...my badbut now i'm getting a "not found" error for $doc->removeChild($element); even though i know "link" is therelook, i'm all for y'all spelling out the code for me (really, i am - i learn SO much better through reverse engineering that it's not even funny), but if y'all know of some good tutorials, i'll happily work on this myselfbut, really, if someone wants to spell out the code, that'd be awesome thanks for the help [Edited on May 6, 2008 at 4:15 PM. Reason : .]
5/6/2008 4:10:37 PM
do a var_dump() on $element and post what it says. you might have to do this instead:
$elements = $doc->getElementsByTagName('link');foreach($elements as $element){... $elements->removeChild($element);...}
5/6/2008 4:26:57 PM
same error, even if i change it to what you suggested
5/6/2008 4:30:07 PM
hmm, what did var_dump have to say?
5/6/2008 4:37:22 PM
nothing...i have this:
$section = "one";$doc = new DOMDocument();if(!$doc->load("toc.xml")) { die("Failed to load XML file.");}foreach($doc->getElementsByTagName('link') as $element) { if(!$element->hasAttribute('section') || strpos($element->getAttribute('section'),$section) == false) { $doc->removeChild($element); }}$filtered_doc = $doc->saveXML();var_dump($element);
5/6/2008 4:48:52 PM
$element is out of scope where you have the var_dump(). move that to the first line inside the for loop. for this little test we really only want to execute it once, so you can also add die('testing'); after the var_dump() so it only does it once.i want to see exactly what $element is.
5/6/2008 4:53:56 PM
object(DOMElement)[3]
5/6/2008 5:00:04 PM
ok, to remove it, you can say:
$element->parentNode->removeChild($element);
$doc->removeChild($element);
5/6/2008 5:51:15 PM
It skips over link6 because it doesn't meet the criteria for removal.
5/6/2008 6:36:08 PM
alright, that seems to have done it...now, my question is how to remove the " section= ... " from the XML documenti put in this:
$filtered_doc = ereg_replace(" section=\"(.*)\"","",$filtered_doc);
5/6/2008 9:23:33 PM
$string = preg_replace('/section="[^"]*"/', '', $string);Actually, http://us3.php.net/manual/en/function.domelement-remove-attribute.php would probably work better.[Edited on May 6, 2008 at 10:05 PM. Reason : .]
5/6/2008 10:04:32 PM
xml parsing is gay
5/7/2008 1:47:58 AM