使用xtract解析xml不适用于嵌套案例

时间:2019-05-20 09:56:19

标签: xml scala xml-parsing

我正在尝试xtract library,并且在解析xml时遇到了困难。

build.sbt:

scalaVersion := "2.12.8"

libraryDependencies ++= Seq(
  "com.lucidchart" %% "xtract" % "2.0.0",
)

我有一个从运动API生成的xml:

<Export>
    <GeneralInformation>
        <Date>2019-05-17</Date>
        <Time>14:21:30</Time>
        <Timestamp>1558102890303</Timestamp>
    </GeneralInformation>
    <Sport code="SOC" id="1" name="SOCCER" hid="13383104" lineup_hid="785967" commentary_hid="1335638">
        <Matchday date="2019-04-14">
            <Match ct="-2" id="1597322" lastPeriod="2 HF" leagueCode="44337" leagueSort="11" leagueType="LEAGUE" startTime="10:00" status="Fin" statustype="fin" type="2" visible="1" lineups="1">
                ...
            </Match>
            <Match ct="-6" id="1599008" lastPeriod="2 HF" leagueCode="44415" leagueSort="11" leagueType="LEAGUE" startTime="10:30" status="Fin" statustype="fin" type="2" visible="1" lineups="1">
                ...
            </Match>
        </Matchday>
    </Sport>
    <Sport code="BSK" id="3" name="BASKETBALL" hid="10716464" lineup_hid="101838" commentary_hid="0">
        <Matchday date="2019-04-14">
            <Match ct="0" id="5181854" lastPeriod="4Qrt" leagueCode="190925" leagueSort="1" leagueType="LEAGUE" startTime="00:00" status="Fin" statustype="fin" type="2" visible="1" lineups="1">
                ...
            </Match>
            <Match ct="0" id="5181855" lastPeriod="4Qrt" leagueCode="190925" leagueSort="1" leagueType="LEAGUE" startTime="02:30" status="Fin" statustype="fin" type="2" visible="1" lineups="1">
                ...
            </Match>
        </Matchday>
    </Sport>
</Export>

按照给出的示例,我具有以下结构化数据:

import com.lucidchart.open.xtract.XmlReader._
import com.lucidchart.open.xtract.{XmlReader, __}
import cats.syntax.all._


case class Sport(
                  code: String,
                  id: String,
                  name: String,
                  hid: String,
                  lineup_hid: String,
                  Matchday: Seq[Matchday]
                )
object Sport {
  implicit val reader: XmlReader[Sport] = (
    attribute[String]("code"),
      attribute[String]("id"),
      attribute[String]("name"),
      attribute[String]("hid"),
      attribute[String]("lineup_hid"),
      (__ \ "Matchday").read(seq[Matchday])
  ).mapN(apply _)
}

case class Matchday (
                      date: String,
                      Matchs: Seq[Match]
                    )
object Matchday {
  implicit val reader: XmlReader[Matchday] = (
    attribute[String]("date"),
      (__ \ "Match").read(seq[Match])
  ).mapN(apply _)
}

case class Match (
                   ct: String,
                   id: String,
                   lastPeriod: String,
                   leagueCode: String,
                   leagueSort: String,
                   leagueType: String,
                   startTime: String,
                   status: String,
                   statusType: String,
                   Type: String,
                   visible: String,
                   lineups: String
                 )
object Match {
  implicit val reader: XmlReader[Match] = (
    attribute[String]("ct"),
      attribute[String]("id"),
      attribute[String]("lastPeriod"),
      attribute[String]("leagueCode"),
      attribute[String]("leagueSort"),
      attribute[String]("leagueType"),
      attribute[String]("startTime"),
      attribute[String]("status"),
      attribute[String]("statusType"),
      attribute[String]("type"),
      attribute[String]("visible"),
      attribute[String]("lineups")
  ).mapN(apply _)
}

case class Export (
                    Sport: Seq[Sport]
                  )
object Export {
  implicit val reader: XmlReader[Export] = (__ \ "Sport").read(seq[Sport]).default(Nil).map(apply _)
}

和我的主应用程序:

import com.lucidchart.open.xtract.XmlReader

import scala.xml.XML

object MainApp {

    val path = "/scorespro.xml"
    private val xml = XML.load(getClass.getResourceAsStream(path))

    def main(args: Array[String]): Unit = {
        println(xml)
        val parsedBlog = XmlReader.of[Export].read(xml)
        println("Parsed Result:")
        println(parsedBlog)
    }
}

但是当我尝试我的代码时,我有这个:

  

***响应:Some(Export(Vector(Sport(SOC,1,SOCCER,13383104,785967,Vector(Matchday(2019-04-14,Vector())))),Sport(BSK,3,BASKETBALL ,10716464,101838,Vector(比赛日(2019-04-14,Vector())))))))

无论我尝试什么-更改Match属性或使用lazyRead-它都不想处理标签。

有什么机会我在这里做错了吗?

0 个答案:

没有答案