Cannot serialize top-level attribute

74 views
Skip to first unread message

Robin Green

unread,
Oct 17, 2013, 7:10:47 AM10/17/13
to scale...@googlegroups.com
I am getting

scales.xml.serializers.CannotSerialize:  class="hr tree"
        at scales.xml.serializers.LSSerializer$class.ct(LSSerializer.scala:241) ~[org.scalesxml.scales-xml_2.10-0.4.5.jar:0.4.5]

which I don't think I used to get before (I'm not sure, because it's a while since I last ran this code).

I thought this might be because a newer version of xerces got pulled in by another dependency, so I force-downgraded that to xerces 2.9.1, but that didn't help.

Another guess: it's because I've tried to add an attribute to a document, instead of a top-level element within that document. The Eclipse debugger seems to indicate that this is indeed the case. How would I fix this? And why doesn't Scales detect the problem as soon as I try to add the attribute - assuming this is really the problem?

Chris Twiner

unread,
Oct 17, 2013, 7:41:16 AM10/17/13
to scale...@googlegroups.com
very strange, have you got a reproducible test case? Need not be a
unit test :)

I'm having pc issues atm, but I'll try to get a look soon.

I'm not even sure how to add an attribute to a document type wise, so
I'm very curious
> --
> You received this message because you are subscribed to the Google Groups
> "scales-xml" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scales-xml+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Robin Green

unread,
Oct 17, 2013, 10:03:59 AM10/17/13
to scale...@googlegroups.com
On Thursday, 17 October 2013 12:41:16 UTC+1, Chris wrote:
very strange, have you got a reproducible test case?

Yes, but it wasn't a case of adding attributes to the document as I first thought. I was on the right lines with downgrading the Xerces dependency though, but I wasn't drastic enough: removing the dependency on Xerces entirely fixes it. Seems your code only works against certain XML implementations (like Java 6's default one).

build.sbt:

libraryDependencies ++= Seq("org.scalesxml" %% "scales-xml" % "0.4.5",
                            "xerces" % "xercesImpl" % "2.9.1")

src/main/scala/Test.scala:

import scales.utils._
import ScalesUtils._
import scales.xml._
import ScalesXml._
import Functions._
import serializers.{LSSerializerConcurrentCacheFactoryXHTML,SerializerData}
import java.nio.charset.Charset
import org.w3c.dom.ls.DOMImplementationLS

object Test extends App {
  def xmlToString[T](doc: T)(implicit ser: SerializeableXml[T]): String = asString(doc)(NoDeclSerializerFactory, implicitly)
  xmlToString(<("html"l)/@("foo" -> "bar"))
}

/** A SerializerFactory which doesn't output XML declarations. */
object NoDeclSerializerFactory extends LSSerializerConcurrentCacheFactoryXHTML {
 
  override def createSerializer( sdata: SerializerData, ndoc : org.w3c.dom.Document) : serializers.LSSerializer = new serializers.XHTMLLSSerializer {
          val data = sdata
          val encMap = encF(sdata.encoding)
          lazy val doc = ndoc
          lazy val impl = doc.getImplementation().asInstanceOf[DOMImplementationLS]
          lazy val lsout = impl.createLSOutput()
          lazy val lsaout = impl.createLSOutput()
          lazy val lss = impl.createLSSerializer()
          override def xmlDeclaration(encoding: Charset, version: XmlVersion) = None
        }

}

Robin Green

unread,
Oct 17, 2013, 10:15:55 AM10/17/13
to scale...@googlegroups.com
On Thursday, 17 October 2013 15:03:59 UTC+1, Robin Green wrote:
On Thursday, 17 October 2013 12:41:16 UTC+1, Chris wrote:
very strange, have you got a reproducible test case?

Yes, but it wasn't a case of adding attributes to the document as I first thought. I was on the right lines with downgrading the Xerces dependency though, but I wasn't drastic enough: removing the dependency on Xerces entirely fixes it.

Unfortunately I am using Play 2.2, which wants Xerces specifically. :(

Robin Green

unread,
Oct 17, 2013, 11:06:47 AM10/17/13
to scale...@googlegroups.com

I've worked around it by creating a JDKDOMFactoryPool which calls DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", null) instead of just DocumentBuilderFactory.newInstance(). This needs to be more easily customisable in future Scales versions without so much copying and pasting.

Chris Twiner

unread,
Oct 17, 2013, 5:39:09 PM10/17/13
to scale...@googlegroups.com
Good to hear you nailed it, I'll add another test project for xerces
2.9.1 to cover that.

Good enough to be a new issue to track -
https://github.com/chris-twiner/scalesXml/issues/31

Chris

unread,
Sep 23, 2014, 3:01:50 AM9/23/14
to scale...@googlegroups.com
FYI for all that landed here via searching for similar errors this is caused due to a lack of Xalan being a dependency.  Add Xalan to the pom / build.sbt and the issue goes away.  A mix between xerces and default sun/oracle jaxp does not seem to function well.



On Thursday, 17 October 2013 23:39:09 UTC+2, Chris wrote:
Good to hear you nailed it, I'll add another test project for xerces
2.9.1 to cover that.

Good enough to be a new issue to track -
https://github.com/chris-twiner/scalesXml/issues/31

On Thu, Oct 17, 2013 at 5:06 PM, Robin Green wrote:
> On Thursday, 17 October 2013 15:15:55 UTC+1, Robin Green wrote:
>>
>> On Thursday, 17 October 2013 15:03:59 UTC+1, Robin Green wrote:
>>>
>>> On Thursday, 17 October 2013 12:41:16 UTC+1, Chris wrote:
>>>>
>>>> very strange, have you got a reproducible test case?
>>>
>>>
>>> Yes, but it wasn't a case of adding attributes to the document as I first
>>> thought. I was on the right lines with downgrading the Xerces dependency
>>> though, but I wasn't drastic enough: removing the dependency on Xerces
>>> entirely fixes it.
>>
>>
>> Unfortunately I am using Play 2.2, which wants Xerces specifically. :(
>
>
> I've worked around it by creating a JDKDOMFactoryPool which calls
> DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",
> null) instead of just DocumentBuilderFactory.newInstance(). This needs to be
> more easily customisable in future Scales versions without so much copying
> and pasting.
>
> --
> You received this message because you are subscribed to the Google Groups
> "scales-xml" group.
> To unsubscribe from this group and stop receiving emails from it, send an
Reply all
Reply to author
Forward
0 new messages