`
sillycat
  • 浏览: 2543195 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

XML and Case Class

    博客分类:
  • JAVA
 
阅读更多
XML and Case Class

I compare the library scalaxb and XStream. It seems to me that XStream is more easy to use.

Here is first step to add the dependencies.
  "com.thoughtworks.xstream" % "xstream" % "1.4.8",
  "org.joda" % "joda-convert" % "1.7",

The Trait object to deal with the xstream base class.
package com.sillycat.jobsconsumer.utilities

import com.sillycat.jobsconsumer.models.Job
import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.io.xml.DomDriver

trait IncludeXMLMapping {

  var xStream : XStream = new XStream(new DomDriver())

  xStream.alias("job", classOf[Job])

  def toXML(obj:Any): String ={
    xStream.toXML(obj)
  }

  def fromXML(xml:String):Any = {
    xStream.fromXML(xml)
  }

}

Here is the Test Class
package com.sillycat.jobsconsumer.models

import com.sillycat.jobsconsumer.utilities.{IncludeXMLMapping}
import org.scalatest.{BeforeAndAfter, Matchers, FunSpec}

class JobXMLSpec extends FunSpec with Matchers with BeforeAndAfter with IncludeXMLMapping {

  describe("JobXMLMapping") {
    describe("#xml2object"){
      it("Convert xml string to object") {
        val xml =
          """<job>
            |  <id>id1</id>
            |  <title>title1</title>
            |  <desc>desc1</desc>
            |  <industry>sales</industry>
            |</job>""".stripMargin
        val job1 = fromXML(xml).asInstanceOf[Job]
        job1.title should be ("title1")
      }
    }
    describe("#object2xml"){
      it("Convert object to xml string") {
        val job1 = Job("id1","title1","desc1","sales")
        val expect =
          """<job>
            |  <id>id1</id>
            |  <title>title1</title>
            |  <desc>desc1</desc>
            |  <industry>sales</industry>
            |</job>""".stripMargin
        val xml = toXML(job1)
        xml should be (expect)
      }
    }
  }

}


Tip
Warning Message
[warn] Class java.time.Duration not found - continuing with a stub.
[warn] Class java.time.Duration not found - continuing with a stub.
[warn] two warnings found

Solution:
Adding package dependency fixes the problem.
"org.joda" % "joda-convert" % "1.7",


References:
http://x-stream.github.io/tutorial.html
http://alvinalexander.com/scala/serializing-deserializing-xml-scala-classes

xml to object
https://github.com/takezoe/solr-scala-client

case class mapper
https://github.com/takezoe/solr-scala-client/blob/master/src/main/scala/jp/sf/amateras/solr/scala/CaseClassMapper.scala
分享到:
评论

相关推荐

    XML and Java - Parsing XML using Java Tutorial [转]

    XML(eXtensible Markup Language)是一种用于标记数据的语言,广泛应用于数据交换、配置文件以及在Web服务中传输数据。Java作为一个强大的编程语言,提供了多种API来解析XML文档,包括DOM(Document Object Model)...

    C#读取XML教程和例子(有代码)

    public class MyClass { public string Name { get; set; } public int Age { get; set; } } ``` 然后,我们可以使用`XmlSerializer`类将XML反序列化为对象: ```csharp using System.Xml.Serialization; // ...

    java在线解析xml

    Java在线解析XML是一种常见的编程任务,特别是在处理Web服务、数据交换或者从XML文件中提取信息时。XML(Extensible Markup Language)是一种结构化数据格式,它允许数据以自定义的标签进行组织,使得数据的传输和...

    java读取XMl数据

    Java语言在处理XML数据时提供了丰富的API,这些API主要集中在`javax.xml`和`org.w3c.dom`等包中。XML(eXtensible Markup Language)是一种结构化数据存储格式,广泛应用于配置文件、数据交换和Web服务等领域。本文...

    android xml多种解析生成

    Android XML 多种解析生成 Android 平台中,XML 文件解析和生成是非常重要的一部分。XML 文件可以存储和交换结构化数据,广泛应用于 Android 应用程序中。本文将介绍 Android 中使用多种解析和生成 XML 文件的方法...

    ios 简单xml解析DEMO绝对可用

    class XMLParser: NSObject, NSXMLParserDelegate { var currentElement = "" var currentValue = "" func parse(xmlData: Data) { let parser = NSXMLParser(data: xmlData) parser.delegate = self parser...

    java四种xml读写demo

    本文将深入探讨Java中处理XML的四种常见方法:DOM(Document Object Model)、SAX(Simple API for XML)、StAX(Streaming API for XML)以及JAXB(Java Architecture for XML Binding)。我们将通过具体的代码示例...

    java解析XML文件

    public class MyHandler extends DefaultHandler { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // 处理开始元素 } @...

    简单java读取xml示例

    public class StaxTest { public static void main(String[] args) throws IOException { // 创建XMLInputFactory对象 XMLInputFactory factory = XMLInputFactory.newInstance(); try (FileInputStream fis = ...

    xml_bjsxt.rar_java xml_xml

    case XMLStreamConstants.START_ELEMENT: // 处理元素开始 break; // 处理其他事件... } } ``` 4. **生成XML文件**: 要生成XML文件,可以使用`javax.xml.transform.Transformer`和`javax.xml.transform....

    java解析xml例子

    在Java编程语言中,XML(eXtensible Markup Language)是一种广泛应用的数据交换格式,用于存储和传输结构化数据。XML的解析是将XML文档转换为Java对象或数据结构的过程,以便程序可以处理这些数据。本篇文章将深入...

    Java与XML(一):采用DOM操作XML文件.docx

    public class DomXML { public void parseXML(String fileName) { try { DocumentBuilder domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputStream input = new FileInputStream...

    用java程序生成mybatis的mapper.xml和mapper.java文件

    例如,`UserMapper userMapper = sqlSession.getMapper(UserMapper.class);` 6. **动态SQL**:Mybatis的mapper.xml文件支持动态SQL,如if、choose、when、otherwise等标签,可以根据条件动态生成SQL语句,增加了SQL...

    ios小程序--解析xml文件,读取新闻

    class XMLParser: NSObject, NSXMLParserDelegate { var currentElement = "" var currentNews = News() func parser(_ parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, ...

    读取xml的几种方法

    public class ReadXMLFile { public static void main(String argv[]) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory....

    java txt文件解析xml格式添加到数据库

    public class SaxHandler extends DefaultHandler { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // 在这里处理开始元素 }...

    java解析xml的4种方法和json格式

    YourClass obj = (YourClass) unmarshaller.unmarshal(new File("yourfile.xml")); // 使用Java对象 System.out.println(obj.getProperty()); ``` 关于JSON格式,JSON是一种轻量级的数据交换格式,易于人阅读和编写...

    java读写xml的代码

    在Java编程中,XML(eXtensible Markup Language)是一种常用的数据交换格式,它具有良好的结构化和可读性,广泛应用于配置文件、数据存储、Web服务等场景。本篇文章将详细探讨如何使用Java进行XML的读写操作,主要...

    java读取xml文件

    Java语言在处理XML文件时提供了多种方法,这些方法可以满足不同场景下的需求。XML(Extensible Markup Language)是一种用于标记数据的语言,广泛应用于数据交换、配置文件存储等场景。本篇将详细介绍Java读取XML...

    java读取XML内容源代码

    public class ReadXMLFile { public static void main(String argv[]) { try { File fXmlFile = new File("example.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); ...

Global site tag (gtag.js) - Google Analytics