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:
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:
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.
Constructor and Description |
---|
XMLRepresentationOfDicomObjectFactory()
Construct a factory object, which can be used to get XML documents from DICOM objects.
|
Modifier and Type | Method and Description |
---|---|
static void |
createDocumentAndWriteIt(AttributeList list,
OutputStream out)
Serialize an XML document (DOM tree) created from a DICOM attribute list.
|
AttributeList |
getAttributeList(Document document)
Given a DICOM object encoded as an XML document
convert it to a list of attributes.
|
AttributeList |
getAttributeList(InputStream stream)
Given a DICOM object encoded as an XML document in a stream
convert it to a list of attributes.
|
AttributeList |
getAttributeList(String name)
Given a DICOM object encoded as an XML document in a named file
convert it to a list of attributes.
|
Document |
getDocument(AttributeList list)
Given a DICOM object encoded as a list of attributes, get an XML document
as a DOM tree.
|
static void |
main(String[] arg)
Read a DICOM dataset and write an XML representation of it to the standard output, or vice versa.
|
static String |
toString(Node node) |
static String |
toString(Node node,
int indent) |
static void |
write(OutputStream out,
Document document)
Serialize an XML document (DOM tree).
|
public XMLRepresentationOfDicomObjectFactory() throws ParserConfigurationException
Construct a factory object, which can be used to get XML documents from DICOM objects.
ParserConfigurationException
public static void createDocumentAndWriteIt(AttributeList list, OutputStream out) throws IOException, DicomException
Serialize an XML document (DOM tree) created from a DICOM attribute list.
list
- the list of DICOM attributesout
- the output stream to write toIOException
DicomException
public AttributeList getAttributeList(Document document) throws DicomException
Given a DICOM object encoded as an XML document convert it to a list of attributes.
document
- the XML documentDicomException
public AttributeList getAttributeList(InputStream stream) throws IOException, SAXException, DicomException
Given a DICOM object encoded as an XML document in a stream convert it to a list of attributes.
stream
- the input stream containing the XML documentIOException
SAXException
DicomException
public AttributeList getAttributeList(String name) throws IOException, SAXException, DicomException
Given a DICOM object encoded as an XML document in a named file convert it to a list of attributes.
name
- the input file containing the XML documentIOException
SAXException
DicomException
public Document getDocument(AttributeList list)
Given a DICOM object encoded as a list of attributes, get an XML document as a DOM tree.
list
- the list of DICOM attributespublic static void main(String[] arg)
Read a DICOM dataset and write an XML representation of it to the standard output, or vice versa.
arg
- either one filename of the file containing the DICOM dataset, or a direction argument (toDICOM or toXML, case insensitive) and an input filenamepublic static void write(OutputStream out, Document document) throws IOException, TransformerConfigurationException, TransformerException
Serialize an XML document (DOM tree).
out
- the output stream to write todocument
- the XML documentIOException
TransformerConfigurationException
TransformerException