Hi, I want to know how to convert CDATA Nodes into Text Nodes While Parsing an XML File? Can anyone help me? I am waiting for your reply. Please help me.
Hi, I want to know how to convert CDATA Nodes into Text Nodes While Parsing an XML File? Can anyone help me? I am waiting for your reply. Please help me.
Hi, for converting CDATA node into text you can use following code in xml. I think this will work for you. Just make use of it. It will help you.
Code:<?xml version="1.0" encoding="UTF-8"?> <!-- Information --> <Company> <Employee Id="Tech-001"> <CompanyName>Techarena</CompanyName> <City>dont</City>> <name>my name</name> <Phoneno>1234567890</Phoneno> <!-- CDATA node --> <Doj>techarena <![CDATA[techarena.in <"!@#$%'^&*()> is in my name]]> Techarena.in </Doj> </Employee> </Company>
Hi, to Convert CDATA Node into Text Node, I have got a java code; which works perfectly as we want. You can use that code to do what you want. So, just use the code as below:
Code:import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; public class CDATANodesTOTextNodes { public static void main(String[] args) throws Exception { File first = new File("Cdata.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setCoalescing(true); DocumentBuilder dbuild = dbf.newDocumentBuilder(); Document doc = dbuild.parse(first); new ConvertingCDATANodesTextNodes().CDATAtextnode(doc); } public void CDATAtextnode(Document doc) throws Exception { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter swriter = new StringWriter(); StreamResult result = new StreamResult(swriter); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlfile = swriter.toString(); System.out.println(xmlfile); } }
Hi, you want to convert cdata node into text node, right? It is very simple with the use of java. You need to use the following small code to get the solution and use it.
Code:import java.io.File; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class Main { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); dbfactory.setCoalescing(true); Document doc = dbfactory.newDocumentBuilder().parse(new File("infilename.xml")); } }
Bookmarks