public class XMLRepresentationOfDicomObjectFactory
extends java.lang.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) {
e.printStackTrace(System.err);
}
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) {
e.printStackTrace(System.err);
}
A typical example of converting XML back to DICOM would be:
try {
AttributeList list = new XMLRepresentationOfDicomObjectFactory().getAttributeList("xmlfile");
list.write(System.out,TransferSyntax.ExplicitVRLittleEndian,true,true);
} catch (Exception e) {
e.printStackTrace(System.err);
}
or if you need to handle the meta information properly:
try {
AttributeList list = new XMLRepresentationOfDicomObjectFactory().getAttributeList("xmlfile");
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) {
e.printStackTrace(System.err);
}
XMLRepresentationOfStructuredReportObjectFactory,
XPathQuery,
com.pixelmed.validate,
Document| 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,
java.io.OutputStream out)
Serialize an XML document (DOM tree) created from a DICOM attribute list.
|
AttributeList |
getAttributeList(org.w3c.dom.Document document)
Given a DICOM object encoded as an XML document
convert it to a list of attributes.
|
AttributeList |
getAttributeList(java.io.InputStream stream)
Given a DICOM object encoded as an XML document in a stream
convert it to a list of attributes.
|
AttributeList |
getAttributeList(java.lang.String name)
Given a DICOM object encoded as an XML document in a named file
convert it to a list of attributes.
|
org.w3c.dom.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(java.lang.String[] arg)
Read a DICOM dataset and write an XML representation of it to the standard output, or vice versa.
|
static java.lang.String |
toString(org.w3c.dom.Node node) |
static java.lang.String |
toString(org.w3c.dom.Node node,
int indent) |
static void |
write(java.io.OutputStream out,
org.w3c.dom.Document document)
Serialize an XML document (DOM tree).
|
public XMLRepresentationOfDicomObjectFactory()
throws javax.xml.parsers.ParserConfigurationException
Construct a factory object, which can be used to get XML documents from DICOM objects.
javax.xml.parsers.ParserConfigurationExceptionpublic org.w3c.dom.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 AttributeList getAttributeList(org.w3c.dom.Document document) throws DicomException
Given a DICOM object encoded as an XML document convert it to a list of attributes.
document - the XML documentDicomExceptionpublic AttributeList getAttributeList(java.io.InputStream stream) throws java.io.IOException, org.xml.sax.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 documentjava.io.IOExceptionorg.xml.sax.SAXExceptionDicomExceptionpublic AttributeList getAttributeList(java.lang.String name) throws java.io.IOException, org.xml.sax.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 documentjava.io.IOExceptionorg.xml.sax.SAXExceptionDicomExceptionpublic static java.lang.String toString(org.w3c.dom.Node node,
int indent)
node - indent - public static java.lang.String toString(org.w3c.dom.Node node)
node - public static void write(java.io.OutputStream out,
org.w3c.dom.Document document)
throws java.io.IOException,
javax.xml.transform.TransformerConfigurationException,
javax.xml.transform.TransformerException
Serialize an XML document (DOM tree).
out - the output stream to write todocument - the XML documentjava.io.IOExceptionjavax.xml.transform.TransformerConfigurationExceptionjavax.xml.transform.TransformerExceptionpublic static void createDocumentAndWriteIt(AttributeList list, java.io.OutputStream out) throws java.io.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 tojava.io.IOExceptionDicomExceptionpublic static void main(java.lang.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 filename