使用PHP从HTML列表生成XML [英] Generating XML from HTML list using PHP

查看:100
本文介绍了使用PHP从HTML列表生成XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在html中转换列表结构:

 < ul> 
< li>第1部分< / li>
< li>第2部分
< ul>
< li>第2.1节< / li>
< li>第2.2节< / li>
< / ul>
< / li>
< li>第3部分< / li>
< / ul>

进入XML如下:

 <段> 
<节>
< caption>第1部分< / caption>
< level> 0< / level>
< / section>
<节>
< caption>第2部分< / caption>
< level> 0< / level>
< / section>
<节>
< caption>第2.1节< / caption>
< level> 1< / level>
< / section>
<节>
< caption>第2.2节< / caption>
< level> 1< / level>
< / section>
<节>
< caption>第3部分< / caption>
< level> 0< / level>
< / section>
< /节>

我尝试使用PHP SimpleXML读取html,但它在遇到< li> < code>标记内的标记。

我想知道是否有人可以好好建议最简单的方法是用PHP完成这项工作吗?



非常感谢大家。

解决方案

您可以随时将HTML解析为XML结构。像这样:



假设您的HTML位于名为sections.html的页面中。这是你可以做你想要做的事情的一种方式:

 <?php 


#创建新的DOM对象
$ domOb = new DOMDocument();

#抓取您的HTML文件
$ html = $ domOb-> loadHTMLFile(sections.html);

#删除空格
$ domOb-> preserveWhiteSpace = false;

#设置容器标签
$ container = $ domOb-> getElementsByTagName('ul');

#通过UL值循环
foreach($ container as $ row)
{
#抓取所有< li>
$ items = $ row-> getElementsByTagName('li');

#回显值
echo $ items-> item(0) - > nodeValue。'< br />';
echo $ items-> item(1) - > nodeValue。'< br />';
echo $ items-> item(2) - > nodeValue;

#您可以写入您的XML文件,以字符串形式存储任何内容
}

?>

我没有测试过这个,但这是一般想法。



希望这会有所帮助。

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.

解决方案

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.

这篇关于使用PHP从HTML列表生成XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆