0

I have a following xml structure which may have any file type like PDF,EXCEL,TEXT files:

 <document>
  <reference>
    <text mediaType="text/plain" representation="B64">
      <reference value="Attachment For Sambar.txt" />
            aGVsbG9fZHVkZS1hd2Vzb21lLnJ1         
    </text>
</document>

Based on the type i need to extract the value(File attachemnt) from reference tag... here is the code i have tried..

NodeList nList = doc.getElementsByTagName("text");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\nCurrent Element :" + nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if(nNode.getNodeName()=="text"){
            System.out.println(eElement.getAttribute("mediaType")+"*&*&");
            System.out.println(eElement.getNodeValue()+"^%%$%%%%%%%%Hhhhhhhhhhhhhhhhh"); 
//NOTE : This throws me null pointer exception
            }
            }
        }

I would like to know how to extract attachments from the reference tag...the above code throws me null pointer exception

Gopi Lal
  • 417
  • 5
  • 23
  • The XML fragement is not well-formed. There is a dangling `reference` tag. – Henry Mar 15 '17 at 05:14
  • And apart from that, you just need a method that decodes base64. Before decoding it, it is just a string from element content, like any other string you get from an XML document. See http://stackoverflow.com/questions/469695/decode-base64-data-in-java – Erwin Bolwidt Mar 15 '17 at 05:16
  • Possible duplicate of [Decode Base64 data in Java](http://stackoverflow.com/questions/469695/decode-base64-data-in-java) – Erwin Bolwidt Mar 15 '17 at 05:16
  • @ErwinBolwidt my question not how to decode Base64String, how do i get that base64String from xml using nNode the above code throws null pointer exception – Gopi Lal Mar 15 '17 at 05:17
  • @Henry this xm..l is auto generated don't i have other options. – Gopi Lal Mar 15 '17 at 05:35
  • @GopiLal in that case the generator is broken, unless you made a mistake copying it. – Henry Mar 15 '17 at 05:39
  • @Henry its been converted using xslt so i need to fix that first is it ?? :( :( – Gopi Lal Mar 15 '17 at 05:42
  • 1
    It is hard to believe an XSLT processor would output something like this. Did you generate it in text output mode? – Henry Mar 15 '17 at 05:45
  • @Henry I see, that wasn't clear from your original question. In that case, you need to find out on which line your NullPointerException occurs, from the stacktrace. How to deal with it is described in the following question: **[What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)** – Erwin Bolwidt Mar 15 '17 at 09:51

0 Answers0