2017-08-11 67 views
0

我有型ELEM的RDD:节约RDD [ELEM]到一个XML文件

val clientXml: RDD[Elem] = parsedClient.filter(s => s.isSuccess).map(s => convertToXML.clientToXML(s.get)) 

这RDD包含类型ELEM元素的集合,每个元素看起来是这样的:

<client> 
    <first>Alexandra</first> 
    <last>Diaz</last> 
    <title></title> 
    <addresses> 
    <address> 
     <type>Home</type> 
     <addr1>3255 Marsh Elder</addr1> 
     <addr2></addr2> 
     <city>La Jolla</city> 
     <province>CA </province> 
     <county>United States</county> 
    </address> 
    </addresses> 
</client> 

我想整个RDD保存到一个XML文件的格式如下:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>. 
    <client> 
     <first>Alexandra</first> 
     <last>Diaz</last> 
     <title></title> 
     <addresses> 
     <address> 
      <type>Home</type> 
      <addr1>3255 Marsh Elder</addr1> 
      <addr2></addr2> 
      <city>La Jolla</city> 
      <province>CA </province> 
      <county>United States</county> 
     </address> 
     </addresses> 
    </client> 

到目前为止,我已成功使用,以节省一个元素以下方法。但我需要的所有元素保存在一个文件中:

val clientElem: Elem = clientXml.treeReduce((a,b) => a) 

XML.save("C:/Temp/Client.xml", clientElem.copy(), "UTF-8", true) 

请注意.saveAsTextFile()是不是我要找的。

val clientXmlList: List[Elem] = for (address <- clientXml.collect().toSeq.toList) yield { 
     address 
    } 

然后创建与嵌入在ElemList[Elem]元素的数据节点:

val clientXmlElemData: Elem = <data> 
    {clientXmlList.map(p => p.copy())} 
</data> 

然后使用XML.write

回答

0

通过将RDD[Elem]List[Elem]解决它()方法写入XML文件:

// create a null DocType so that the docType is not inserted to the output XML file 
val doctype = null 

// create a FileWriter which writes to a file "C:/Temp/Client.xml" 
val file = new File("C:/Temp/Client.xml") 

// create a BufferedWriter to write to the file "C:/Temp/Client.xml" 
val bw = new BufferedWriter(new FileWriter(file)) 

// write the clientXmlElemData node to the file setting write xml declaration to true 
XML.write(bw, clientXmlElemData, "UTF-8", true, doctype) 

// close the BufferedWriter after the file has been created 
bw.close() 
相关问题