1

I have this code:

org.w3c.dom.Document doc = docBuilder.parse(representation.getStream());
                Element element = doc.getDocumentElement();
                NodeList nodeList = element.getElementsByTagName("xnat:MRSession.scan.file");
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Node node = nodeList.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        // do something with the current element

my problem is with getElementsByTagName("xnat:MRSession.scan.file")

my xml looks like this:

<?xml version="1.0" encoding="UTF-8"?><xnat:MRSession "REMOVED DATA IGNORE">
<xnat:sharing>
<xnat:share label="23_MR1" project="BOGUS_GSU">
<!--hidden_fields[xnat_experimentData_share_id="1",sharing_share_xnat_experimentDa_id="xnat_E00001"]-->
</xnat:share>
</xnat:sharing>
<xnat:fields>
<xnat:field name="studyComments">
<!--hidden_fields[xnat_experimentData_field_id="1",fields_field_xnat_experimentDat_id="xnat_E00001"]-->S</xnat:field>
</xnat:fields>
<xnat:subject_ID>xnat_S00002</xnat:subject_ID>
<xnat:scanner manufacturer="GE MEDICAL SYSTEMS" model="GENESIS_SIGNA"/>
<xnat:prearchivePath>/home/ryan/xnat_data/prearchive/BOGUS_OUA/20120717_131900137/23_MR1</xnat:prearchivePath>
<xnat:scans>
<xnat:scan ID="1" UID="1.2.840.113654.2.45.2.108830" type="SAG LOCALIZER" xsi:type="xnat:mrScanData">
<!--hidden_fields[xnat_imageScanData_id="1"]-->
<xnat:image_session_ID>xnat_E00001</xnat:image_session_ID>
<xnat:quality>usable</xnat:quality>
<xnat:series_description>SAG LOCALIZER</xnat:series_description>
<xnat:scanner manufacturer="GE MEDICAL SYSTEMS" model="GENESIS_SIGNA"/>
<xnat:frames>29</xnat:frames>
<xnat:file URI="/home/ryan/xnat_data/archive/BOGUS_OUA/arc001/23_MR1/SCANS/1/DICOM/scan_1_catalog.xml" content="RAW" file_count="29" file_size="3968052" format="DICOM" label="DICOM" xsi:type="xnat:resourceCatalog">

So Basically I need to be able to iterate through all the xnat:MRSession/xnat:scan/xnat:file elements and make some changes. Problem is

getElementsByTagName("xnat:MRSession.scan.file")

Is always null. Please help. Thanks

rpfujiw
  • 57
  • 1
  • 10
  • 2
    What in the documentation makes you think it should work the way you're using it? – parsifal Aug 07 '12 at 19:18
  • The documentation is unclear to me. I just need to know what the proper syntax is to specific the xnat:file element. Any light you can shine on this would be very helpful. Obviously I'm missing something. – rpfujiw Aug 07 '12 at 19:22
  • 1
    It seems to me that what you need is xPath, not `getElementsByTagName`. – Andrea Bergia Aug 07 '12 at 19:33
  • xpath huh? Any example you can point me to? – rpfujiw Aug 07 '12 at 19:45
  • @user1332868 Google and the javadocs should get you started there. That said, I'd use an alternate DOM like JDOM in this case, the W3C API is fairly awkward when it comes to XPath. – millimoose Aug 07 '12 at 19:49
  • andrea thanks I'll get started there – rpfujiw Aug 07 '12 at 19:55
  • ok so this is what I cam up with: `Representation representation = representItem(exp.getItem(),MediaType.TEXT_XML); Document doc = new SAXBuilder().build(representation.getStream()); XPath xpath = XPath.newInstance("/xnat:MRSession/xnat:scan/xnat:file"); ` This give me an InvocationTaretException This looks like it should work – rpfujiw Aug 07 '12 at 22:20
  • the error occurs when SAXBuilder is instantiated – rpfujiw Aug 07 '12 at 22:22
  • Here is an example using the `javax.xml.xpath` APIs in the JDK/JRE with namespaces: http://stackoverflow.com/questions/11644994/parse-xml-with-namespaces-in-java-using-xpath/11647407#11647407 – bdoughan Aug 08 '12 at 01:14

1 Answers1

1

You could try the following using XPath:

Document document = // the parsed document
XPathFactory xPathFactory = XPathFactory.newInstance();
NodeList allFileNodes = xPathFactory.newXPath().evaluate("\\XNAT_NAMESPACE:file", document.getDocumentElement(), XPathConstants.NODESET);

Instead XNAT_NAMESPACE you would need to specify the exact namespace that is meant with the prefix "xnat" in your example.

bourbert
  • 308
  • 3
  • 14