I am trying to import data from an XML file and then perform calculations on those numeric values. When I import the data the values seem to be stored as strings. I tried using a ToExpression function on the different fields in a Cases function but the values seem to stay as strings.
So first off here is a sample of my XML file:
<ArrayOfXMLHexNode>
<XMLHexNode>
<ID>1</ID>
<X>-1.000000000000000e+003</X>
<Y>0.000000000000000e+000</Y>
<Radius>1.000000000000000e+003</Radius>
<Angle>3.141592653589793e+000</Angle>
<RingIndex>0.000000000000000e+000</RingIndex>
<RingSuborder>1.000000000000000e+000</RingSuborder>
<DisplayOrder>1</DisplayOrder>
</XMLHexNode>
<XMLHexNode>
<ID>2</ID>
<X>1.000000000000000e+003</X>
<Y>0.000000000000000e+000</Y>
<Radius>1.000000000000000e+003</Radius>
<Angle>0.000000000000000e+000</Angle>
<RingIndex>0.000000000000000e+000</RingIndex>
<RingSuborder>1.000000000000000e+000</RingSuborder>
<DisplayOrder>2</DisplayOrder>
</XMLHexNode>
<XMLHexNode>
<ID>3</ID>
<X>-5.000000000000000e+002</X>
<Y>-8.660000000000000e+002</Y>
<Radius>1.000000000000000e+003</Radius>
<Angle>4.188790204786391e+000</Angle>
<RingIndex>0.000000000000000e+000</RingIndex>
<RingSuborder>1.000000000000000e+000</RingSuborder>
<DisplayOrder>3</DisplayOrder>
</XMLHexNode>
</ArrayOfXMLHexNode>
And an example of how I am importing the data follows:
hexNodeData = Import["HexNodes.xml"]
myHexNodes :=
Cases[hexNodeData, XMLElement["XMLHexNode", _, _], Infinity];
pointsWithDisplayOrder =
Cases[myHexNodes,
XMLElement[
"XMLHexNode", _, {___, XMLElement["X", {}, {x_}], ___,
XMLElement["Y", {}, {y_}], ___,
XMLElement["RingSuborder", {}, {subOrder_}], ___,
XMLElement[
"DisplayOrder", {}, {displayOrder_}], ___}] -> {ToExpression[x],
ToExpression[y], ToExpression[subOrder],
ToExpression[displayOrder]}, Infinity]
And then a few lines to try out the data I imported:
In[53]:= tempDO1 = pointsWithDisplayOrder[[3]][[3]]
Out[53]= "1.000000000000000e+000"
In[54]:= tempDO1 \[Element] Reals
Out[54]= "1.000000000000000e+000" \[Element] Reals
In[55]:= StringMatchQ[tempDO1, "1.000000000000000e+000"]
Out[55]= True
In[56]:= ToExpression[tempDO1, InputForm]
Out[56]= 1. e
In[57]:= tempDO = ToExpression[pointsWithDisplayOrder[[3]][[4]]]
Out[57]= 3
In[58]:= tempDO \[Element] Reals
Out[58]= True
So a couple questions come out of this.
Why does the ToExpression's used in the Cases call not convert my data to Reals or Integers as appropiate when I store the data in the pointsWithDisplayOrder List? And to be clear I mean in the first code block where I have a line like this:
...-> {ToExpression[x], ToExpression[y], ToExpression[subOrder], ToExpression[displayOrder]}...
How can I convert values in sql scientific notation like 5.0000e+000 into a Real Number in Mathematica? Mathematica wants to treat e is an unknown variable.
Thanks in advance.
Brian
ImportString
for this kind of data, therefore does without the massive overhead of that function and is a great deal faster. It is an undocumented internal function however, so I leave you to choose between the compromises of all three methods (ImportString
,ToExpression
,ParseTable
). $\endgroup$