Here we import your data as symbolic XML:
In[50]:= xml = Import["C:\\Temp\\matrixData.xml"]
Out[50]= XMLObject["Document"][{},
XMLElement["data", {},
{XMLElement["row", {"key" -> "1", "val1" -> "A", "val2" -> "10"},
{XMLElement["occ", {}, {"100"}], XMLElement["occ", {}, {"20"}],
XMLElement["occ", {}, {"30"}]}],
XMLElement["row", {"key" -> "2", "val1" -> "B", "val2" -> "0"},
{XMLElement["occ", {}, {"100"}]}],
XMLElement[ "row", {"key" -> "3", "val1" -> "X","val2" -> "0"},
{XMLElement["occ", {}, {"120"}], XMLElement["occ", {}, {"20"}]}],
XMLElement["row", {"key" -> "4", "val1" -> "C", "val2" -> "11"}, {}]}], {}]
Here we parse into a matrix:
In[51]:= matr =
xml /. XMLObject["Document"][{}, data_, _] :> data /.
XMLElement["data", _, children_] :> children /.
XMLElement["row", attrs_, vals_] :> {"key" /. attrs,
"val1" /. attrs, vals /. XMLElement["occ", _, {val_}] :> val,
"val2" /. attrs} /. s_String :> ToExpression[s]
Out[51]= {{1, A, {100, 20, 30}, 10}, {2, B, {100}, 0}, {3,X, {120, 20}, 0}, {4, C, {}, 11}}
The code is short and economical due to the use of rule-within-a-rule technique. I discuss it here. One nice application of this technique can be found here.
Here is the reverse:
XMLObject["Document"][{}, XMLElement["data", {},
Replace[matr, {key_, val1_, vals_List, val2_} :>
XMLElement[ "row", {"key" -> ToString[key], "val1" -> ToString[val1],
"val2" -> ToString[val2]},
XMLElement["occ", {}, {ToString[#]}] & /@ vals], {1}]], {}]
I do not show the result but it is the same as the imported symbolic XML. You can call Export
with this symbolic XML and it will know how to handle it.