By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

retain markup when importing from XML?

P: 37
Howdy folks,

My XML file contains the contents of several web pages. Example:

Expand|Select|Wrap|Line Numbers
  1. <allPages>
  3. <thisPage>
  4. <h1>Page Title</h1>
  5. <p>Some text, which contains an <a href="link.php">inline link.</a></p>
  6. </thisPage>
  8. <anotherPage>
  9. <p>you get the <b>idea</b></p>
  10. </anotherPage>
  12. </allPages>
Text snippets from several different nodes of my XML file will be displayed on the homepage, followed by a link to the full page, which will likewise read the XML file and display the contents.

I thought this would be a really great idea and very simple to implement, but there's a problem: my inline links do not display.

Actually, the first version of my PHP script didn't import any of the page elements at all, it just spat out a plaintext blob with no markup whatsoever. So I revised it and now the script reads:

Expand|Select|Wrap|Line Numbers
  1. $myXML = new DOMDocument();
  2. $myXML->load('includes/content.xml');
  4. $aboutNode=$myXML->getElementsByTagName('about')->item(0);
  5. $start = $aboutNode->firstChild;
  6. $content = "";//initialize the variable as an empty string
  8. for($i=0;$i<$aboutNode->childNodes->length; $i++){
  9. if($start->nodeType !=3){//if the node is an element
  10.     $snt = $start->nodeName;
  11.     $content .= '<'.$snt.'>';
  12.     $content .= $start->nodeValue;
  13.     $content .= '</'.$snt.'>';
  14.     }
  15. $start=$start->nextSibling;
  16. }
  18. echo $content;//display the output
(I'm not including the function that truncates the returned value because I'm not having a problem with that portion of the script)

And that worked great! Except it still does not deal with my inline links. They are missing. Apparently markup elements (such as inline links) are not treated as child nodes by the parser?

In JavaScript you can just use innerHTML to achieve this. In my Google quest I found a PHP script on someone's blog that supposedly emulates the innerHTML function but it was very complicated. It seems like this process must be done all the time without resorting to complex workarounds.

So my question is, what's the simplest way to import a node's contents, including markup? I can't figure it out. Thanks very much!

Sep 2 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,639
I would go for XSL, there you can copy and output whole nodesets[1], plus it uses only a few lines of php.

[1] using the <xsl:copy-of> element
Sep 2 '08 #2

P: 37
Thanks Dormilich!

XSL is not something I have studied up to this point; but if it's what I will need in order to do the type of coding described above, then I guess I will have to make it my next learning project.

I appreciate the feedback.
Sep 2 '08 #3

P: 37
So somebody helped me figure out how to do this using php's simplexml functions

that's more appropriate for this project, because a. since my truncate function is also in php, i can keep these together in one script, and b. learning XSLT might take me a while...

should anyone find this via a google search I think you'll find the following helpful.

Expand|Select|Wrap|Line Numbers
  1. $myXML = new SimpleXMLElement(realpath(dirname(__FILE__)).'/includes/content.xml', null, true);
  3. $content_nodes = $myXML->xpath('//about');
  4. $content = '';
  5. foreach($content_nodes as $node)
  6. {
  7. $content .= $node->asXML();
  8. }
  10. echo $content; 
have a great day!
Sep 4 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.