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

9
  • 2
    What in the documentation makes you think it should work the way you're using it? Commented Aug 7, 2012 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. Commented Aug 7, 2012 at 19:22
  • 1
    It seems to me that what you need is xPath, not getElementsByTagName. Commented Aug 7, 2012 at 19:33
  • xpath huh? Any example you can point me to? Commented Aug 7, 2012 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. Commented Aug 7, 2012 at 19:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.