2017-07-04 7 views
0

xmlをjsonに変換するサンプルプログラムを作成しました。しかし、私は文字の後にシーケンスを削除したい。それを達成する方法。Scalaは、シーケンスの後の文字列から文字を削除します。

以下の例では、 "<"文字の後に来る場合は、文字列内の "tns:"を削除したいと考えています。

object example { 
def main(args: Array[String]): Unit = { 
    val xmldata :String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tns:createGroup xmlns:tns=\"http://www.emirates.com/schemas/loyalty/customer/group/v1.0/2017/05\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.emirates.com/schemas/loyalty/customer/group/v1.0/2017/05 sample.xsd \">\n <tns:customerGroup>\n <tns:groupTypeCode>100</tns:groupTypeCode>\n <tns:groupTypeDescription>sampleGroup</tns:groupTypeDescription>\n <tns:groupAccountNumber>100100234</tns:groupAccountNumber>\n <tns:groupName>sampleName</tns:groupName>\n <tns:status>open</tns:status>\n <tns:memberCountThreshold>3</tns:memberCountThreshold>\n <tns:guestCountThreshold>4</tns:guestCountThreshold>\n <tns:creatorDetails>\n  <tns:membershipNumber>00800934564</tns:membershipNumber>\n  <tns:mediaCode>400</tns:mediaCode>\n </tns:creatorDetails>\n <tns:createdDate>03JUL2017</tns:createdDate>\n <tns:MilesSummary/>\n </tns:customerGroup>\n <tns:error>\n <tns:errorRecord>noerror</tns:errorRecord>\n </tns:error>\n</tns:createGroup>\n" 
    val loyaltyGroupMessageXML = scala.xml.XML.loadString(xmldata) 
    val miles = (loyaltyGroupMessageXML \\ "customerGroup") 
    //print(miles) 
    val json = toJson(miles) 
    //val parsed = parse(json) 
    //print(parsed) 
    print(pretty(render(json))) 
    } 
} 
+0

は「」あなたは後の文字列を切り捨てるしたいですか? – shanmuga

+0

はい....私はtnsだけを削除したい – user2526641

答えて

0

あなたはあなたがまだ</に終わる</tns:を交換する必要が

val loyaltyGroupMessageXML = scala.xml.XML.loadString(xmldata.replace("<tns:", "<")) 

として、あなたのコードでそれを使用することができますreplace機能

xmldata.replace("<tns:", "<") 

を使用することができます。そのために、あなたのコードでは

xmldata.replace("<tns:", "<").replace("</tns:", "</") 

を行うことができます

val loyaltyGroupMessageXML = scala.xml.XML.loadString(xmldata.replace("<tns:", "<").replace("</tns:", "</")) 
関連する問題