Following Code is trying to read an XML and convert the Element Name and values as | sepeared Key|val pairs.
If val contains HTML tags, the code should return val as HTML AS IS
import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Example { public String invoke(String InputXML) throws Exception { String a = InputXML.replace("<", "<"); String xml = a.replace(">", ">"); InputStream in = new ByteArrayInputStream(xml.getBytes("utf-8")); Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(in); String Elements = ""; String Values = ""; String ElementValues = ""; Node objects = doc.getDocumentElement(); for (Node object = objects.getFirstChild(); object != null; object = object .getNextSibling()) { if (object instanceof Element) { Element e = (Element) object; if (Elements.length() == 0 && Values.length() == 0) { String s1 = e.getTagName(); if (s1.contains(":")) { String[] split = s1.split(":"); Elements = split[1]; } else { Elements = s1; } Values = e.getTextContent(); ElementValues = Elements + "|" + e.getTextContent(); } else { String s2 = e.getTagName(); if (s2.startsWith("ns") && s2.contains(":")) { String[] split = s2.split(":"); s2 = split[1]; } else { s2 = s2; } Elements = Elements + "," + s2; Values = Values + "," + e.getTextContent(); ElementValues = ElementValues + "," + s2 + "|" + e.getTextContent(); } } } return Elements + "\n" + ElementValues ; } } Test Codeprivate static void TestInp1Sample() { Example parseXML = new Example(); try { String inp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:InputXML xmlns:ns0=\"http://www" + "Application" + "Schema/Schema.xsd\"><ns0:Name>VV,YY-AWUAH</ns0:Name><" + "ns0:MName> <B> ABCD </B> EFGH </ns0:MName>" + "<ns0:LName>__XXXX__</ns0:LName>" + "</ns0:InputXML>"; System.out.println(parseXML.invoke(inp)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } **Expected response** Name,MName,LName Name|LORRAINE,OFORI-AWUAH,MName| <B> ABCD </B> EFGH ,LName|__XXXX__ **Actual** Name,MName,LName Name|LORRAINE,OFORI-AWUAH,MName| ABCD EFGH ,LName|__XXXX__ Any solution? I can not add text in CDATA and can not use other java libraries
No comments:
Post a Comment