4

I would like to convert the list structure in html:

<ul>
    <li>Section 1</li>
    <li>Section 2
        <ul>
            <li>Section 2.1</li>
            <li>Section 2.2</li>
        </ul>
    </li>
    <li>Section 3</li>
</ul>

Into XML like this:

<sections>
    <section>
        <caption>Section 1</caption>
        <level>0</level>
    </section>
    <section>
        <caption>Section 2</caption>
        <level>0</level>
    </section>
    <section>
        <caption>Section 2.1</caption>
        <level>1</level>
    </section>
    <section>
        <caption>Section 2.2</caption>
        <level>1</level>
    </section>
    <section>
        <caption>Section 3</caption>
        <level>0</level>
    </section>
</sections>

I tried to use PHP SimpleXML to read in the html but it seems to have problem when it encounters an <ul> tag inside a <li> tag.

I wonder if someone can kindly suggest what the simplest way is to get this done in PHP?

Many thanks to you all.

2
  • 5
    Have you considered using XSLT? This is exactly what it does. Commented Feb 2, 2011 at 23:48
  • 1
    I think SimpleXML's problem is the mix of text and other elements at <li>Section 2... Commented Feb 2, 2011 at 23:59

1 Answer 1

4

You could always just parse that HTML into your XML structure. Something like this:

Let's assume your HTML is in a page called "sections.html". This is one way you could do what you're looking to do:

<?php


  # Create new DOM object
  $domOb = new DOMDocument();

  # Grab your HTML file
  $html = $domOb->loadHTMLFile(sections.html);

  # Remove whitespace
  $domOb->preserveWhiteSpace = false; 

  # Set the container tag
  $container = $domOb->getElementsByTagName('ul'); 

  # Loop through UL values
  foreach ($container as $row) 
  { 
      # Grab all <li>
      $items = $row->getElementsByTagName('li'); 

      # echo the values  
      echo $items->item(0)->nodeValue.'<br />'; 
      echo $items->item(1)->nodeValue.'<br />'; 
      echo $items->item(2)->nodeValue;

      # You could write to your XML file, store in a string, anything here
    } 

?>

I haven't tested this, but that's the general idea.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.