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

  • Home
  • SEARCH
  • 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 6817205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:01:17+00:00 2026-05-26T21:01:17+00:00

I am trying to parse some fairly flat HTML and group everything from one

  • 0

I am trying to parse some fairly flat HTML and group everything from one h1 tag to the next. For example, I have the following HTML:

<h1> Heading 1 </h1>
<p> Paragraph 1.1 </p>
<p> Paragraph 1.2 </p>
<p> Paragraph 1.3 </p>
<h1> Heading 2 </h1>
<p> Paragraph 2.1 </p>
<p> Paragraph 2.2 </p>
<h1> Heading 3 </h1>
<p> Paragraph 3.1 </p>
<p> Paragraph 3.2 </p>
<p> Paragraph 3.3 </p>

I basically want it to look like:

<div id='1'>
    <h1> Heading 1 </h1>
    <p> Paragraph 1.1 </p>
    <p> Paragraph 1.2 </p>
    <p> Paragraph 1.3 </p>
</div>
<div id='2'>
    <h1> Heading 2 </h1>
    <p> Paragraph 2.1 </p>
    <p> Paragraph 2.2 </p>
</div>
<div id='3'>
    <h1> Heading 3 </h1>
    <p> Paragraph 3.1 </p>
    <p> Paragraph 3.2 </p>
    <p> Paragraph 3.3 </p>
</div>

It is probably not even worth be posting the code I have done so far, as it just turned into a mess. Basically I was attempting to do an Xpath query for ‘//h1’. Create new DIV tags as parent nodes. Then copy the h1 DOM Node into the first DIV, and then loop over nextSibling until I hit another h1 tag – as mentioned it got messy.

Could someone point me in a better direction here?

  • 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-05-26T21:01:18+00:00Added an answer on May 26, 2026 at 9:01 pm

    Iterate over all nodes that are on the same level (I created a hint node called platau in my example), whenever your run across <h1>, insert the div before and keep a reference to it.

    For <h1> and any other node and if the reference exists, remove the node and add it as child to the reference.

    Example:

    $doc->loadXML($xml);
    $xp = new DOMXPath($doc);
    
    $current = NULL;
    $id = 0;
    foreach($xp->query('/platau/node()') as $i => $sort)
    {
        if (isset($sort->tagName) && $sort->tagName === 'h1')
        {
            $current = $doc->createElement('div');
            $current->setAttribute('id', ++$id);
            $current = $sort->parentNode->insertBefore($current, $sort);
        }
        if (!$current) continue;
    
        $sort->parentNode->removeChild($sort);
        $current->appendChild($sort);
    }
    

    Demo

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

Sidebar

Related Questions

I'm trying to parse some returned html (from http://www.google.com/movies?near=37130 )to look for currently playing
I have some HTML I am trying to parse. There are cases where the
I am trying to parse some output data from and PBX and I have
I was trying to parse some HTML content from a site. Nokogiri works perfectly
I'm trying to use Nokogiri to parse an HTML file with some fairly eccentric
I am trying to parse some XML i have retrieved via e4x in an
Trying to parse an HTML document and extract some elements (any links to text
I'm trying to parse some XML (html) I downloaded using WebRequest.Create() and then read
I'm trying to parse some text using PyParser. The problem is that I have
I'm trying to parse some HTML with QT / C++. However, I don't want

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.