/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.utils; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.SAXException; /** * * @author raido */ public class XMLHandler { private Document doc = null; public XMLHandler(String data) { if ( !data.isEmpty() ) { try { InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8")); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = builder.parse(is); } catch (SAXException ex) { Logger.getLogger(XMLHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(XMLHandler.class.getName()).log(Level.SEVERE, null, ex); } catch(ParserConfigurationException e) { } } } public boolean parseSuccess() { return ( this.doc == null ? false : true ); } public String getNodeValue(String node) { String nodeValue = null; if ( doc != null ) { nodeValue = doc.getElementsByTagName(node).item(0).getTextContent(); } return nodeValue; } public Long getNodeValueLong(String node) { String nodeValue = null; if ( doc != null ) { nodeValue = doc.getElementsByTagName(node).item(0).getTextContent(); if ( nodeValue != null ) { return Long.parseLong( nodeValue ); } } return 0L; } }