I am new to clojure, so please bear with me. I have an XML that looks like
<?xml version="1.0" encoding="UTF-8"?> <XVar Id="cdx9" Type="Dictionary"> <XVar Id="Base.AccruedPremium" Type="Multi" Value="" Rows="1" Columns="1"> <Row Id="0"> <Col Id="0" Type="Num" Value="0"/> </Row> </XVar> <XVar Id="TrancheAnalysis.IndexDuration" Type="Multi" Value="" Rows="1" Columns="1"> <Row Id="0"> <Col Id="0" Type="Num" Value="3.4380728252313069"/> </Row> </XVar> <XVar Id="TrancheAnalysis.IndexLevel01" Type="Multi" Value="" Rows="1" Columns="1"> <Row Id="0"> <Col Id="0" Type="Num" Value="30693.926279941188"/> </Row> </XVar> <XVar Id="TrancheAnalysis.TrancheDelta" Type="Multi" Value="" Rows="1" Columns="1"> <Row Id="0"> <Col Id="0" Type="Num" Value="8.9304387917502073"/> </Row> </XVar> <XVar Id="TrancheAnalysis.TrancheDuration" Type="Multi" Value="" Rows="1" Columns="1"> <Row Id="0"> <Col Id="0" Type="Num" Value="3.0775955481964035"/> </Row> </XVar> </XVar>
And this is repeated. From this I want to be able to create a CSV file with these columns
IndexName,TrancheAnalysis.IndexDuration,TrancheAnalysis.TrancheDuration cdx9,3.4380728252313069,3.0775955481964035 ......................................... .........................................
I can parse a simple XML file like
<?xml version="1.0" encoding="UTF-8"?> <CalibrationData> <IndexList> <Index> <Calibrate>Y</Calibrate> <UseClientIndexQuotes>Y</UseClientIndexQuotes> <IndexName>HYCDX10</IndexName> <Tenor>06/20/2013</Tenor> <TenorName>3Y</TenorName> <IndexLevels>219.6</IndexLevels> <Tranche>Equity0To0.15</Tranche> <TrancheStart>0</TrancheStart> <TrancheEnd>0.15</TrancheEnd> <UseBreakEvenSpread>1</UseBreakEvenSpread> <UseTlet>0</UseTlet> <IsTlet>0</IsTlet> <PctExpectedLoss>0</PctExpectedLoss> <UpfrontFee>52.125</UpfrontFee> <RunningFee>0</RunningFee> <DeltaFee>5.3</DeltaFee> <CentralCorrelation>0.1</CentralCorrelation> <Currency>USD</Currency> <RescalingMethod>PTIndexRescaling</RescalingMethod> <EffectiveDate>06/17/2011</EffectiveDate> </Index> </IndexList> </CalibrationData>
with this code
(ns DynamicProgramming (:require [clojure.xml :as xml])) ;Get the Input Files (def calibrationFile "C:/ashwani/Eclipse/HistoricalTrancheAnalysis/src/CalibrationQuotes.xml") (def mktdataFile "C:/ashwani/Eclipse/HistoricalTrancheAnalysis/src/MarketData.xml") (def sample "C:/ashwani/Eclipse/HistoricalTrancheAnalysis/src/Sample.xml") ;Parse the Calibration Input File (def CalibOp (for [x (xml-seq (xml/parse (java.io.File. calibrationFile))) :when (or (= :IndexName (:tag x)) (= :Tenor (:tag x)) (= :UpfrontFee (:tag x)) (= :RunningFee (:tag x)) (= :DeltaFee (:tag x)) (= :IndexLevels (:tag x)) (= :TrancheStart (:tag x)) (= :TrancheEnd (:tag x)) )] (first(:content x)))) (println CalibOp)
But the second XML is simple; on the other hand, I donβt know how to iterate through the nested structure of the first XML example and extract the necessary information.
Any help would be great.
xml clojure
Ash Jun 24 2018-11-11T00: 00Z
source share