Generating XML from HTML list using PHP

By | July 12, 2018
Questions:

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.

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *