How to return image data of an xml file with java

urban_man :

Hi I'm using dom to parse xml files mostly through the use of getTextContent, but for some reason I can't get the text from the picture tag and I'm not sure why

public String image(){
     {
         try {
             builder = factory.newDocumentBuilder();
             Document doc= builder.parse(filename);
             NodeList messageList=doc.getElementsByTagName("picture");
             for(int i=0;i<messageList.getLength();i++){
                 Node node=messageList.item(i);
                 if(node.getNodeType()==Node.ELEMENT_NODE){
                     Element element=(Element) node;
                     System.out.println(element.getTextContent());

                 }
             }


         } catch (ParserConfigurationException e) {
             e.printStackTrace();
         } catch (SAXException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
        }
     return "error";

 }

this is an example xml file

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <text>can't get picture tag to work zzz</text>
    <picture data="iVBORw0KGgoAAAANSUhEUgAAKXRFWHRDcAAHdElNRQfk6CIIiPoZwD+ALXGFxj6BgYeU7BO4tToSDFHYWZ2+/c03OzPZDRJNYcgVwG4hZQOLPeF24ZkCe6ZxDCOqHcmxmsr+hsicahss+n8vYb8NHZPTJxi/RGC5IqbRwqH6uxVTX+5LvHtvT/V/R6PGh/iF4GHoBAwz7RD26spwq6Amh/AAAAAElFTkSuQmCC" />
</test>
Lkopo :

That's because you are trying to get content of the empty tag. What you want is the value of the data attribute, which can be obtained as follows:

element.getAttributes().getNamedItem("data").getNodeValue()

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409848&siteId=1