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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:13:23+00:00 2026-05-15T08:13:23+00:00

Dear All, I am trying to parse the following HTML fragment, and I would

  • 0

Dear All, I am trying to parse the following HTML fragment, and I would like to get the same fragment as output (without HTML and BODY tags). Is this possible? If so, how?

Thank you
Misha

p.s. I am reading here:
http://nekohtml.sourceforge.net/faq.html#fragments
and I believe I have added the correct options below. However, the output is still incorrect 🙁

Thank you
Misha

import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
import groovy.util.XmlNodePrinter
import groovy.util.slurpersupport.NodeChild


def text="""
<div><h2>Test</h2>
<div>Hi</div>
</div>
"""

// Parse
def config=new org.cyberneko.html.HTMLConfiguration()
config.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment",true)
def html=new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parseText(text)          

// Output
def printNode(NodeChild node) {
    def writer = new StringWriter()
    writer << new StreamingMarkupBuilder().bind {
        mkp.declareNamespace('':node[0].namespaceURI())
        mkp.yield node
    }
    new XmlNodePrinter().print(new XmlParser().parseText(writer.toString()))
}
printNode(html)

Output:

<HTML>
  <tag0:HEAD xmlns:tag0="http://www.w3.org/1999/xhtml"/>
  <BODY>
    <DIV>
      <H2>
        Test
      </H2>
      <DIV>
        Hi
      </DIV>
    </DIV>
  </BODY>
</HTML>
  • 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-15T08:13:24+00:00Added an answer on May 15, 2026 at 8:13 am

    Call setFeature on the parser object directly, like so:

    @Grab(group='net.sourceforge.nekohtml', module='nekohtml', version='1.9.14')
    
    import groovy.xml.MarkupBuilder
    import groovy.xml.StreamingMarkupBuilder
    import groovy.util.XmlNodePrinter
    import groovy.util.slurpersupport.NodeChild
    
    
    def text="""
    <div><h2>Test</h2>
    <div>Hi</div>
    </div>
    """
    
    // Parse
    def parser=new org.cyberneko.html.parsers.SAXParser()
    parser.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment",true)
    def html=new XmlSlurper(parser).parseText(text)          
    
    // Output
    def printNode(NodeChild node) {
        def writer = new StringWriter()
        writer << new StreamingMarkupBuilder().bind {
            mkp.declareNamespace('':node[0].namespaceURI())
            mkp.yield node
        }
        new XmlNodePrinter().print(new XmlParser().parseText(writer.toString()))
    }
    printNode(html)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello all i got a mail code like this body = Dear & cName
Dear Scholars I am trying to parse a RSS feed with the following item
Dear all, I have a select query that currently produces the following results: DoctorName
dear all..i have a table it looks like: Name version DDX 01 DTX 05
Dear all,I am now using a webtool http://fiddesktop.cs.northwestern.edu/mmp/scrape?url= to parse a webpage. For example,we
Dear All, A newbie to XPATH, I need an xpath to get the HREF
Dear All, I am trying to achieve svn copy operation with the help of
Dear All, i have a txt file like: --start-- jeff --end-- --start-- sophya --end--
Dear all, Given a variable that takes on, say, three values, I'm trying to
Dear Android hackers, I am trying to do the following in my Android App:

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.