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

appending xml Elements recursively

1 reply
Robby Pelssers
Joined: 2010-03-20,
User offline. Last seen 42 years 45 weeks ago.

Hi,

I was wondering what I needed to change to following code snippet in order to get it to work properly:

case class FileComponent(file: File) extends TreeComponent[File, FileComponent]{

def isComposite = file.isDirectory

def subComponents = for {
aFile <- file.listFiles
} yield FileComponent(aFile)

def component = FileComponent(file)
def value = file

def toXML: Elem = {

{value.getName()}
{
if (isComposite) {

{
subComponents.foreach(_.toXML) //this is not appending the elements to their parent... but I don’t know how to reference the context node in order to append it's children
}

}
}

}
}

Kind regards,
Robby Pelssers

Robby Pelssers
Joined: 2010-03-20,
User offline. Last seen 42 years 45 weeks ago.
RE: appending xml Elements recursively

Ok... I just found the answer :-)

case class FileComponent(file: File) extends TreeComponent[File, FileComponent]{

def isComposite = file.isDirectory

def subComponents = for {
aFile <- file.listFiles
} yield FileComponent(aFile)

def component = FileComponent(file)
def value = file

def toXML: Elem = {

{value.getName()}
{
if (isComposite) {

{
for { subComponent <- subComponents } yield subComponent.toXML
}

}
}

}

}

-----Original Message-----
From: Robby Pelssers [mailto:robby.pelssers@ciber.com]
Sent: Friday, March 26, 2010 3:13 PM
To: scala-user@listes.epfl.ch
Subject: [scala-user] appending xml Elements recursively

Hi,

I was wondering what I needed to change to following code snippet in order to get it to work properly:

case class FileComponent(file: File) extends TreeComponent[File, FileComponent]{

def isComposite = file.isDirectory

def subComponents = for {
aFile <- file.listFiles
} yield FileComponent(aFile)

def component = FileComponent(file)
def value = file

def toXML: Elem = {

{value.getName()}
{
if (isComposite) {

{
subComponents.foreach(_.toXML) //this is not appending the elements to their parent... but I don’t know how to reference the context node in order to append it's children
}

}
}

}
}

Kind regards,
Robby Pelssers

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