This page is no longer maintained — Please continue to the home page at www.scala-lang.org

XML Entity and scala.XML.loadString

2 replies
mailleux
Joined: 2008-08-23,
User offline. Last seen 4 years 7 weeks ago.


I have been puzzled by this behavior, when I use the Scala parser to read XML I can load an XML Entity without problem:

scala> <b>&nbsp;</b>
res1: scala.xml.Elem = <b>&nbsp;</b>

scala> res1.child(0).getClass.getSimpleName
res2: java.lang.String = EntityRef

However if I try to read using the scala.xml.XML.loadString I get:

scala> scala.xml.XML.loadString(res1.toString)
org.xml.sax.SAXParseException: The entity "nbsp" was referenced, but not declare
d.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAX
ParseException(Unknown Source)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalErro
r(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(
Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(U
nknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImp
l.scanEntityReference(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImp
l$FragmentContentDriver.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(U
nknown Source)
        at com.sun.org.apache.xe...

I'm using the loadString and load(file) to read snippets of HTML converted to XML (not really XHTML). I would love to have the &nbsp; entities as EntityRef, but I dont know the correct way. Any help on how to do this?

Thomas
Roshan Dawrani
Joined: 2010-07-20,
User offline. Last seen 42 years 45 weeks ago.
Re: XML Entity and scala.XML.loadString
This thread seems to be discussing the same issue and has some suggestions: http://osdir.com/ml/lang.scala/2008-01/msg00298.html .

See if it helps.

On Wed, Aug 4, 2010 at 5:44 PM, Thomas Sant'ana <mailleux@gmail.com> wrote:


I have been puzzled by this behavior, when I use the Scala parser to read XML I can load an XML Entity without problem:

scala> <b>&nbsp;</b>
res1: scala.xml.Elem = <b>&nbsp;</b>

scala> res1.child(0).getClass.getSimpleName
res2: java.lang.String = EntityRef

However if I try to read using the scala.xml.XML.loadString I get:

scala> scala.xml.XML.loadString(res1.toString)
org.xml.sax.SAXParseException: The entity "nbsp" was referenced, but not declare
d.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAX
ParseException(Unknown Source)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalErro
r(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(
Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(U
nknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImp
l.scanEntityReference(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImp
l$FragmentContentDriver.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(U
nknown Source)
        at com.sun.org.apache.xe...

I'm using the loadString and load(file) to read snippets of HTML converted to XML (not really XHTML). I would love to have the &nbsp; entities as EntityRef, but I dont know the correct way. Any help on how to do this?

Thomas

huynhjl
Joined: 2009-10-27,
User offline. Last seen 42 years 45 weeks ago.
Re: XML Entity and scala.XML.loadString

You can try to prefix your XML snippets with:

<?xml version="1.0" encoding="utf-8"?>
]>

Depending on how much of the HTML universe you have to deal with, I don't
know if this approach would work.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland