开发者

Generating XML from HTML list using PHP

开发者 https://www.devze.com 2023-02-08 00:39 出处:网络
I would like to convert the list structure in html: <ul> <li>Section 1</li> <li>Section 2

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>
        <capt开发者_StackOverflowion>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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消