User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » PHP and XML question Page [1]  
quagmire02
All American
44225 Posts
user info
edit post

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 start

let'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>


so when you select section one, you'd see

link01
link02
link03
link04
subsection
link08
link09


does that make sense? is it possible to do it like that, to have the "section" attribute of <link> contain multiple entries and then have PHP display only those entries whose attribute match the one i'm trying to display?

i went over the XML tutorial at w3schools, and i did a basic google search (as well as checking out the php.net site), but largely what i'm seeing is how to generate XML from a PHP script, which isn't what i'm looking to do...suggestions?

5/6/2008 12:42:04 PM

DirtyMonkey
All American
4269 Posts
user info
edit post

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

quagmire02
All American
44225 Posts
user info
edit post

i keep getting:

Fatal error: Call to undefined method DOMDocument::getElementByTagName()

5/6/2008 3:35:07 PM

Shadowrunner
All American
18332 Posts
user info
edit post

plural Elements

getElementByTagName is JavaScript

5/6/2008 3:39:23 PM

qntmfred
retired
40726 Posts
user info
edit post

i think it's plural in javascript too, eh?

5/6/2008 3:58:34 PM

ablancas
All American
575 Posts
user info
edit post

or in .NET land, how about XMLDocument class

5/6/2008 4:07:50 PM

quagmire02
All American
44225 Posts
user info
edit post

well, damnation...my bad

but now i'm getting a "not found" error for $doc->removeChild($element); even though i know "link" is there

look, 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 myself

but, 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

DirtyMonkey
All American
4269 Posts
user info
edit post

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);
...
}


[Edited on May 6, 2008 at 4:27 PM. Reason : s]

5/6/2008 4:26:57 PM

quagmire02
All American
44225 Posts
user info
edit post

same error, even if i change it to what you suggested

5/6/2008 4:30:07 PM

DirtyMonkey
All American
4269 Posts
user info
edit post

hmm, what did var_dump have to say?

5/6/2008 4:37:22 PM

quagmire02
All American
44225 Posts
user info
edit post

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);


and it craps out...what obvious thing am i missing? it's been a long day, and i'm sure i'm gonna feel dumb when it's pointed out to me

5/6/2008 4:48:52 PM

DirtyMonkey
All American
4269 Posts
user info
edit post

$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

quagmire02
All American
44225 Posts
user info
edit post

object(DOMElement)[3]

5/6/2008 5:00:04 PM

DirtyMonkey
All American
4269 Posts
user info
edit post

ok, to remove it, you can say:


$element->parentNode->removeChild($element);

INSTEAD OF

$doc->removeChild($element);


which works for link5, but for some reason it skips over link6. i'm not sure why but i think it's because we removed link5 from the document. see if you can get any further and i'll check back later. i gotta head out for a bit.

[Edited on May 6, 2008 at 5:51 PM. Reason : instead of]

5/6/2008 5:51:15 PM

Stein
All American
19842 Posts
user info
edit post

It skips over link6 because it doesn't meet the criteria for removal.

Quote :
"but now i'm getting a "not found" error for $doc->removeChild($element); even though i know "link" is there"


For clarification of why this doesn't work, you have to be familiar with the fact that getElementsByTagName goes throughout the entire document, so that it finds every link tag, including link08, link09, and link10.

removeChild requires the parentNode of the element you want to remove to make the removal call. So the issue you're running into is that $doc->removeChild($element) will work for link01 through link07, but it'll choke at link08, since $doc isn't the parentNode.

DirtyMonkey's $element->parentNode->removeChild($element); should work though as it makes a direct call to $element's parent to make the removal.

[Edited on May 6, 2008 at 6:36 PM. Reason : .]

5/6/2008 6:36:08 PM

quagmire02
All American
44225 Posts
user info
edit post

alright, that seems to have done it...now, my question is how to remove the " section= ... " from the XML document

i put in this:

$filtered_doc = ereg_replace(" section=\"(.*)\"","",$filtered_doc);


so that it would remove the "section" attribute and its identifiers, regardless of whether it's "one two" or "one three" or "one two three"...this, though, is getting rid of the all except the first link, and also the <a href="#"> tag

what am i missing?

5/6/2008 9:23:33 PM

Stein
All American
19842 Posts
user info
edit post

$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

skokiaan
All American
26447 Posts
user info
edit post

xml parsing is gay

5/7/2008 1:47:58 AM

 Message Boards » Tech Talk » PHP and XML question Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.