Class XMLRepresentationOfDicomObjectFactory

java.lang.Object
com.pixelmed.dicom.XMLRepresentationOfDicomObjectFactory

public class XMLRepresentationOfDicomObjectFactory extends Object

A class to encode a representation of a DICOM object in an XML form, suitable for analysis as human-readable text, or for feeding into an XSLT-based validator, and to convert them back again.

An example of the type of output produced by this class is as follows:

<?xml version="1.0" encoding="UTF-8"?>
  <DicomObject>
    <FileMetaInformationGroupLength element="0000" group="0002" vr="UL">
      <value number="1">222</value>
    </FileMetaInformationGroupLength>
    ...
    <ImageType element="0008" group="0008" vr="CS">
      <value number="1">ORIGINAL</value>
      <value number="2">PRIMARY</value>
      <value number="3">CINE</value>
      <value number="4">NONE</value>
    </ImageType>
    ...
    <ContrastBolusAgentSequence element="0012" group="0018" vr="SQ">
      <Item number="1">
        <CodeValue element="0100" group="0008" vr="SH">
          <value number="1">C-17800</value>
        </CodeValue>
      ...
      </Item>
    </ContrastBolusAgentSequence>
    ...
    <PixelData element="0010" group="7fe0" vr="OW"/>
</DicomObject>
 

There are a number of characteristics of this form of output:

  • Rather than a generic name for all DICOM data elements, like "element", with an attribute to provide the human-readable name, the name of the XML element itself is a human-readable keyword, as used in the DICOM Data Dictionary for the toolkit; the group and element tags are available as attributes of each such element; this makes construction of XPath accessors more straightforward.
  • The value representation of the DICOM source element is conveyed explicitly in an attribute; this facilitates validation of the XML result (e.g., that the correct VR has been used, and that the values are compatible with that VR).
  • Individual values of a DICOM data element are expressed as separate XML elements (named "value"), each with an attribute ("number") to specify their order, starting from 1 increasing by 1; this prevents users of the XML form from needing to parse multiple string values and separate out the DICOM value delimiter (backslash), and allows XPath accessors to obtain specific values; it also allows for access to separate values of binary, rather than string, DICOM data elements, which are represented the same way. Within each "value" element, the XML plain character data contains a string representation of the value.
  • Sequence items are encoded in a similar manner to multi-valued attributes, i.e., there is a nested XML data element (called "Item") with an explicit numeric attribute ("number") to specify their order, starting from 1 increasing by 1.

E.g., to test if an image is original, which is determined by a specific value of ImageType (0008,0008), one could write in XPath "/DicomObject/ImageType/value[@number=1] = 'ORIGINAL'". To get the code value of the contrast agent in use, one could write "/DicomObject/ContrastBolusAgentSequence/Item[@number=1]/CodeValue/value[@number=1]", or making some assumptions about cardinality and depth of nesting and removing the predicates, simply "//ContrastBolusAgentSequence/Item/CodeValue/value". One could do this from the command line with a utility such as XPathQuery.

Note that a round trip from DICOM to XML and back again does not result in full fidelity, since:

  • Binary floating point values will lose precision when converted to string representation and back again
  • Leading and trailing white space and control characters in strings will be discarded
  • Meta information header elements will be changed
  • Structural elements such as group lengths will be removed and may or may not be replaced
  • Physical offsets such as in the DICOMDIR will be invalidated
  • Attributes with OB and OW value representations have their values discarded so as not to encode the bulk pixel data (probably should be added as an option)

A typical example of how to invoke this class to convert DICOM to XML would be:

try {
    AttributeList list = new AttributeList();
    list.read("dicomfile",null,true,true);
    Document document = new XMLRepresentationOfDicomObjectFactory().getDocument(list);
    XMLRepresentationOfDicomObjectFactory.write(System.out,document);
} catch (Exception e) {
    slf4jlogger.error("",e);
 }
 

or even simpler, if there is no further use for the XML document:

try {
    AttributeList list = new AttributeList();
    list.read("dicomfile",null,true,true);
    XMLRepresentationOfDicomObjectFactory.createDocumentAndWriteIt(list,System.out);
} catch (Exception e) {
    slf4jlogger.error("",e);
 }
 

A typical example of converting XML back to DICOM would be:

try {
    AttributeList list = new XMLRepresentationOfDicomObjectFactory().getAttributeList("xmlfile");
    list.insertSuitableSpecificCharacterSetForAllStringValues();
    list.write(System.out,TransferSyntax.ExplicitVRLittleEndian,true,true);
} catch (Exception e) {
    slf4jlogger.error("",e);
 }
 

or if you need to handle the meta information properly:

try {
    AttributeList list = new XMLRepresentationOfDicomObjectFactory().getAttributeList("xmlfile");
    list.insertSuitableSpecificCharacterSetForAllStringValues();
    String sourceApplicationEntityTitle = Attribute.getSingleStringValueOrEmptyString(list,TagFromName.SourceApplicationEntityTitle);
    list.removeMetaInformationHeaderAttributes();
    FileMetaInformation.addFileMetaInformation(list,TransferSyntax.ExplicitVRLittleEndian,sourceApplicationEntityTitle);
    list.write(System.out,TransferSyntax.ExplicitVRLittleEndian,true,true);
} catch (Exception e) {
    slf4jlogger.error("",e);
 }
 

When the XML is being converted to DICOM, the group, element and VR attributes are not needed if the element name is a keyword that can be found in the dictionary; if they are present, then their values are checked against the dictionary values.

See Also: