473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

retain markup when importing from XML?

Howdy folks,

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

Expand|Select|Wrap|Line Numbers
  1. <allPages>
  2.  
  3. <thisPage>
  4. <h1>Page Title</h1>
  5. <p>Some text, which contains an <a href="link.php">inline link.</a></p>
  6. </thisPage>
  7.  
  8. <anotherPage>
  9. <p>you get the <b>idea</b></p>
  10. </anotherPage>
  11.  
  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');
  3.  
  4. $aboutNode=$myXML->getElementsByTagName('about')->item(0);
  5. $start = $aboutNode->firstChild;
  6. $content = "";//initialize the variable as an empty string
  7.  
  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. }
  17.  
  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!

-thesmithman
Sep 2 '08 #1
3 1361
Dormilich
8,658 Expert Mod 8TB
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
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
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);
  2.  
  3. $content_nodes = $myXML->xpath('//about');
  4. $content = '';
  5. foreach($content_nodes as $node)
  6. {
  7. $content .= $node->asXML();
  8. }
  9.  
  10. echo $content; 
have a great day!
Sep 4 '08 #4

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

Similar topics

30
by: Reinhold Birkenfeld | last post by:
Hello, I know that there are different YAML engines for Python out there (Syck, PyYaml, more?). Which one do you use, and why? For those of you who don't know what YAML is: visit...
38
by: Jukka K. Korpela | last post by:
As well all know, valid markup is important... but when trying to find a convincing modern argument in favor of this, I found pages like http://www.htmlhelp.com/tools/validator/reasons.html which...
4
by: crhaynes | last post by:
I'm having trouble with my CSS. My links are black, my hover is orange and my active link is red. When I select a link it turns red but i does not retain that color when the selected page loads. ...
52
by: Andy Dingley | last post by:
I'm using this at present: <p title="Publication date" ></p> Works fine on screen, but Fangs/Jaws just reads it as "left bracket twenty-eight slash zero slash two thousand five fifteen colon...
2
by: Bill Cohagan | last post by:
In my app I'm validating an XML file against an XSD which contains several attribute default value specifications. I'm performing the validation via an xml document load() using a...
1
by: KC | last post by:
Hi, I created a new event log source and log and user WriteEntry to enter the log as follows: ' Create the source, if it does not already exist. If Not EventLog.SourceExists("MySource") Then...
3
by: Karl | last post by:
Hi there, I am using asp.net with c#. I created a web user control "MyControl". When I click a button, page creates an instance of "MyControl" and loads it at run time. It is good. And the...
2
by: afr0ninja | last post by:
Hello! I'm fairly new to access, but I'm starting to get the hang of it. This will be a bit lengthy but hopefully it'll have enough information for some kind soul to help me out. I'm currently...
3
by: raaman rai | last post by:
Hi Fellas, i need to understand how to retain the submtted form values during an Edit/Update operation. When i add the information from a form, i retain the form values in the following way incase...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.