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 4627292
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:32:49+00:00 2026-05-22T03:32:49+00:00

I use scala.xml.PrettyPrinter to format my XML in Scala. The problem is with nodes

  • 0

I use scala.xml.PrettyPrinter to format my XML in Scala. The problem is with nodes without text content. Instead of this:

<node></node>

I’d prefer to have this:

<node />

How can I make PrettyPrinter to format it my way?

  • 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-22T03:32:50+00:00Added an answer on May 22, 2026 at 3:32 am

    This was a bug in scala-xml, but it was fixed in version 1.1.0 on Feb-20-2018. A new option minimizeEmpty was added to PrettyPrinter.

    To use 1.1.0, add the following to your build.sbt:

    libraryDependencies ++= Seq(
      "org.scala-lang.modules" %% "scala-xml" % "1.1.0"
    )
    

    Here’s an example of how to make use of the new option in PrettyPrinter:

    val pp = new xml.PrettyPrinter(80, 2, minimizeEmpty = true)
    val x = <node><leaf></leaf></node>
    println(pp.format(x))
    

    This will output:

    <node>
      <leaf/>
    </node>
    

    If the Scala compiler, complains:

    java.lang.NoSuchMethodError: scala.xml.PrettyPrinter.<init>(IIZ)V
    

    then you need to enable a forked JVM in sbt, so that Scala is using the new version of scala-xml. Just add the follow to your build.sbt:

    fork := true
    

    Previous to scala-xml 1.1.0, the method to create <node/>, leafTag(), is in the class, but unused. You can fix it like so:

    import xml._
    val p2 = new PrettyPrinter(120, 2) {
      override protected def traverse(node:Node, pscope:NamespaceBinding, ind:Int) = 
        node match {
          case n:Elem if n.child.size == 0 => makeBox(ind, leafTag(n))
          case _ => super.traverse(node, pscope, ind)
        }
    }
    

    There’s no reason to use the override-hack if you can just upgrade to 1.1.0.

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

Sidebar

Related Questions

I am trying to generate some XML on-the-fly in Scala. I want to use
I have a Scala representation of some XML (i.e. a scala.xml.Elem ), and I'd
I am trying to parse this document in scala: <?xml version=1.0?> <model> <joint name=pelvis>
I have a scala project that I use Maven and the maven-scala-plugin to compile.
This code: println(new XStream.toXML(List(1,2,3))) produces this XML: <scala.coloncolon serialization=custom> <unserializable-parents/> <scala.coloncolon> <int>1</int> <int>2</int> <int>3</int>
I prefer to use OOP in large scale projects like the one I'm working
Use case: 3rd party application wants to programatically monitor a text file being generated
I use emacs to edit my xml files (nxml-mode) and the files were generated
How do you load an HTML DOM document into Scala? The XML singleton had
Everyone who's done any web application development in Scala knows that you can use

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.