Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:36:50+00:00 2026-06-13T22:36:50+00:00

I have an XML file which contains text with some very simple layout constructs:

  • 0

I have an XML file which contains text with some very simple layout constructs:

<?xml version='1.0'?>
<page>
  <section>
    <header>Header</header>
    <par>Some paragraph</par>
    <par>Another paragraph with <emph>formatting</emph></par>
  </section>
</page>

In PHP then I read this file using SimpleXML (Note that I intentionally strip other tags!):

$page = file_get_contents("page.xml");
if ($page) {
  $stripped = strip_tags($page, "<?xml><page><section><header><par><emph>");
  $xml = new SimpleXMLElement($stripped);
}

Now I would like to iterate over the XML elements and print them in order as HTML for my website. The final result should be the following snippet:

<h1>Header</h1>
<p>Some paragraph
<p>Another paragraph with <i>formatting</i>

I’ve noodled through SimpleXML and XPath and tried to figure out how I can iterate over the XML tree in order so that I can digest the original XML file into HTML output. I can produce a somewhat desired result but the <emph></emph> is just gone; how do I descent further into the tree? My code so far:

foreach ($xml->section as $s) {
  echo "<h1>" . $s->header . "</h1>";
  foreach ($s->par as $p) {
    echo "<p>" . $p;
    //  Do some magic here to ensure <emph> tags are recognized and responded to properly.
  }
}

Any hints and pointers are appreciated! Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:36:52+00:00Added an answer on June 13, 2026 at 10:36 pm

    Well, without an answer I just had to noodle myself 🙂 So here is what I did and it worked out just fine.

    Turned out that the SimpleXML thing didn’t cut it, so I used the XMLReader:

    $xml = new XMLReader();
    

    Then I manually parsed the XML string, jumped from element to element and acted upon each of them:

    if ($xml->xml($stripped)) { // $stripped here is a string that's been validated (see below).
      while (false !== $xml->read()) {
        $t = $xml->nodeType;
        if ($t === XMLReader::ELEMENT) {
          $n = $xml->name;
          switch ($n) {
            case "page":
            case "section":
              // Nothing to echo here.
              break;
            case "header":
              // Handle attributes here
              echo "<h1>";
              break;
            case "par":
              echo "<p> ";
              break;
            case "emph";
              echo "<i>"; // This can also open a <span> for more flexibility later.
              break;
            default:
              // Nothing should arrive here.
              echo "Gah!"
          }
        }
        else if ($t === XMLReader::END_ELEMENT) {
          ... // Close the opened tags here.
        }
        else if ($t === XMLReader::TEXT) {
          $s = $xml->readString();
          echo $s;
        }
        else {
          // Everything else are comments or white spaces.
        }
      }
    }
    

    You get the drift. I basically had to bounce through the XML structure myself and, dependent on the element type, handle attributes and nodes of elements manually.

    In fact, this is a two-step process. What you see here assumes a valid XML document. I also have a validator that runs before the above code, and which makes sure that the correct elements are nested properly and that the given XML is “well formed” as per my own definitions of nesting, attributes, whatnot. The validator operates after the exact same principle.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file which contains some data as given. <?xml version=1.0 encoding=UTF-8
i have xml file which contains some details i want to pick some value
I have written xml file which contains html tags as element like <component> <input
I have a xml file which contains arabic characters.When i try to parse a
I have an XML file of(30GB) which contains 2 classes of data, The data
I have a problem parsing a XML file which contains special characters like ,
I have a XML-file, which contains the data of a calendar. I want to
I have a large XML file which in the middle contains the following: <ArticleName>Article
I have this xml file which has text init. i.e Hi my name is
i have a log file which contains hundreds/thousands of seperate XML messages and need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.