Search code examples
javaxmlxsltjaxbcdata

How to retrievea particular tag value from the cdata section using xsl


My XML is like bellow

<?xml version="1.0" encoding="UTF-8"?>
<Students>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?> <Student><rno>1</rno><name>xyz</name>     </student>]]>
</Students>

with the help of XSL i want to retrieve the value of rno which is present inside cdata section. How I can read this value


Solution

  • First of all, in your xml you should replace </student> by </Student> . Because XML tags are case sensitive reference here.

    The trick to do that on your own is the following :

    public static String getRNO(){
    
        String valueRetrieved = null;
    
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        Document doc = null;
    
        try {
            db = dbf.newDocumentBuilder();
            doc = db.parse(new File("D:\\Loic_Workspace\\Test2\\res\\test.xml"));
            NodeList ndList = doc.getElementsByTagName("Students");
            String xmlRetrieved = ndList.item(0).getTextContent();
    
            if(xmlRetrieved != null) {
                //CALL OF STRING REPLACE METHOD TO PREVENT FROM
                //at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
                //at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source
                xmlRetrieved = xmlRetrieved.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
    
                doc = db.parse(new InputSource(new StringReader(xmlRetrieved)));
                valueRetrieved = doc.getElementsByTagName("rno").item(0).getTextContent();
            }
    
    
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    
        return valueRetrieved;
    
    }
    

    Hope it helps ;-)