This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / Add-ons & Links / XML Capabilities / Importing XML /

Import

You can import XML data into Mathematica using the standard Import function, which has the following syntax.

    Import[file]

    Import[file, format]

The first argument of the function specifies the file to be imported. You can also specify an optional second argument to control the form of the output. For importing XML data, the relevant file formats are: "XML", "NotebookML", "ExpressionML", "MathML", and "SymbolicXML".

With "XML" as the import format, any XML formats that Mathematica does not recognize are returned as SymbolicXML. The three formats that Mathematica does support on import, namely NotebookML, ExpressionML, and MathML, are treated slightly differently. A NotebookML file is imported as a notebook expression. An ExpressionML file is imported as the corresponding cell expression. A MathML file is returned as the corresponding box expression.

With "SymbolicXML" as the import format, even NotebookML, ExpressionML, and MathML files are imported as SymbolicXML. This gives you a way to override any interpretation beyond just importing as SymbolicXML.

Here is a file containing a simple MathML equation.

In[1]:=

Out[1]=<math xmlns="http://www.w3.org/1998/Math/MathML">
<semantics>
<msup>
<mi>x</mi>
<mn>2</mn>
</msup>
<annotation-xml encoding="MathML-Content">
<apply>
<power/>
<ci>x</ci>
<cn type="integer">2</cn>
</apply>
</annotation-xml>
</semantics>
</math>

If you import the file and specify "XML" as the second argument, the equation is converted into a box expression.

In[2]:=

Out[2]=

If you specify "SymbolicXML" as the second argument, the equation is imported as a SymbolicXML box expression.

In[3]:=

Out[3]=

If Import is used with only one argument, Mathematica processes the data in the file based on its file extension. Any file with a .xml extension is imported as XML. This means that if it is in one of the XML formats explicitly supported by Mathematica, namely NotebookML, ExpressionML, or MathML the file will be interpreted in the appropriate way. All other XML formats are imported as SymbolicXML.

Mathematica also recognizes the .mml extension for MathML files and the .nbml extension for NotebookML files. In the following example, we import a file with the .mml extension.

In[4]:=

Out[4]=

You can display the above box expression as conventional mathematical notation by using DisplayForm.

In[5]:=

Out[5]//DisplayForm=

You can control the various details of the import process, such as how to treat whitespace, whether to recognize entities, or whether to validate against a DTD, by specifying conversion options to the Import function. For more information, see XML Import Conversion Options.