Child pages
  • SAXParseException when submitting XML to XNAT
Skip to end of metadata
Go to start of metadata

Problem

When submitting XML to XNAT via REST, you receive an error message similar to this:

SAXParseException
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 130; Element type "xnat:QCAssessment" must be followed by either attribute specifications, ">" or "/>".

Solution

This error occurs due to invalid XML, but the cause may be either actual invalid XML (in which case the solution is to fix the XML) or the formatting of the XML. Specifically, when processing the XML text, the XNAT server will read each line of the incoming request body, concatenating each line into a single block of XML. If there are line feeds or returns in your XML in a place that requires whitespace between XML elements, those elements may end up not being separated by whitespace.

For example, consider the following XML describing a standard XNAT QC assessment.

Sample QC assessment
<?xml version="1.0" encoding="UTF-8"?> <xnat:QCAssessment ID="XNAT_E00183_QC" type="4dfp_DICOM_QC" project="PRJ001"
label="PRJ001_001_MR1_QC" xsi:schemaLocation="http://nrg.wustl.edu/xnat xnat.xsd" xmlns:xnat="http://nrg.wustl.edu/xnat"
xmlns:prov="http://www.nbrin.net/prov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <xnat:date>2014-08-13</xnat:date>
<xnat:imageSession_ID>XNAT_E00183</xnat:imageSession_ID> </xnat:QCAssessment>

Each line is terminated by a line feed, with no other whitespace. That can then be sent to XML via curl or some other command:

curl command
curl -u admin:admin -X PUT -d @qc.xml -H "Content-Type:application/xml" http://server/xnat/data/archive/projects/PRJ001/subjects/PRJ001_001/experiments/PRJ001_001_MR1/assessors/PRJ001_001_MR1_QC

This will result in a SAXParseException message similar to the one above. The problem is at the end of the lines. Look at the end of the first line and the beginning of the second line. Those are concatenated, but no whitespace is added, resulting in the following:

Concatenated XML
<?xml version="1.0" encoding="UTF-8"?> <xnat:QCAssessment ID="XNAT_E00183_QC" type="4dfp_DICOM_QC" project="PRJ001"label="PRJ001_001_MR1_QC"

Note that project="PRJ001" and label="PRJ001_001_MR1_QC" run right into each other. This is the cause of the SAXParseException error.

To fix this issue, you have a few options:

  • End all lines with an empty space. Incoming line feeds are removed, but not other whitespace. This will ensure that elements in the XML remain distinct.
  • Ensure that all XML elements are on the same line, so that a line feed doesn't break the element up.
  • Use a pretty-print or other XML formatting option when generating or composing XML.

Once you've eliminated this problem, valid and well-formed XML should import properly into XNAT.