Java: How to get xml nodes path

hotlikeme

New Member
I have following xml: \[code\]<?xml version="1.0" encoding="ISO-8859-1"?><RootNode> <Node_11>LEVEL_1-Value_1</Node_11> <Node_12>LEVEL_1-Value_2</Node_12> <Node_13> <Node_121>LEVEL_2-Value_1</Node_121> </Node_13> <Node_14> <Node_121> <Node_1231> <Node_12341>LEVEL_4-Value_1</Node_12341> <Node_12342>LEVEL_4-Value_2</Node_12342> <Node_12343>LEVEL_4-Value_3</Node_12343> </Node_1231> </Node_121> </Node_14> <Node_15> <Node_25> <Node_251>Value_251</Node_251> <Node_252>Value_252</Node_252> </Node_25> <Node_25> <Node_251>Value_253</Node_251> <Node_252>Value_254</Node_252> </Node_25> <Node_25> <Node_251>Value_255</Node_251> <Node_252>Value_256</Node_252> </Node_25> <Node_25> <Node_251>Value_257</Node_251> <Node_252>Value_258</Node_252> </Node_25> </Node_15></RootNode> \[/code\]I have to print nodes path with value using java. Here is the sample of output I have to get: \[code\]RootNode.Node_11 = LEVEL_1-Value_1RootNode.Node_12 = LEVEL_1-Value_2RootNode.Node_13.Node_121 = LEVEL_2-Value_1RootNode.Node_14.Node_121.Node_1231.Node_12341 = LEVEL_4-Value_1RootNode.Node_14.Node_121.Node_1231.Node_12342 = LEVEL_4-Value_2RootNode.Node_14.Node_121.Node_1231.Node_12343 = LEVEL_4-Value_3RootNode.Node_15.Node_25.Node_251 = Value_251RootNode.Node_15.Node_25.Node_252 = Value_252RootNode.Node_15.Node_25.Node_251 = Value_253RootNode.Node_15.Node_25.Node_252 = Value_254RootNode.Node_15.Node_25.Node_251 = Value_255RootNode.Node_15.Node_25.Node_252 = Value_256RootNode.Node_15.Node_25.Node_251 = Value_257RootNode.Node_15.Node_25.Node_252 = Value_258\[/code\]Here is my last java code. I cannot get it work correctly. \[code\]public class Read_XML { static String rootTag = ""; static HashMap valueMap = new HashMap(); public static void main(String arg[]) throws IOException, AxiomRuntimeException { File inFile = new File("C:/Test.xml"); FileReader fr = new FileReader(inFile); BufferedReader br = new BufferedReader(fr); String sXML = ""; for (String line; (line = br.readLine())!= null;) { sXML += line; } Document doc = XMLStringParser.parseString(sXML); Element root = doc.getDocumentElement(); rootTag += root.getTagName(); NodeList rootList = doc.getElementsByTagName("RootNode"); Element rootList_node = (Element) rootList.item(0); NodeList kids = rootList_node.getChildNodes(); for(int i = 0; i < kids.getLength(); i++) { String nextTag = ""; if(kids.item(i) instanceof Element) { nextTag = rootTag + "." + kids.item(i).getNodeName(); int level = 0; processChildNode(kids.item(i).getChildNodes(), nextTag, level); } } Iterator it = valueMap.keySet().iterator(); for (;it.hasNext();) { String key = it.next().toString(); String val = valueMap.get(key).toString(); System.out.println(key + " = " + val ); }}public static void processChildNode(NodeList listOfNodes, String tags, int level){ String tagPath = tags; int curLevel = 0; for(int i = 0; i < listOfNodes.getLength(); i++) { System.out.println("Node Name = " + listOfNodes.item(i).getNodeName()); if(listOfNodes.item(i) instanceof Element) { if(curLevel == level + 1) { tagPath = tags; } else { curLevel = level +1; tagPath += "." + listOfNodes.item(i).getNodeName(); } if(listOfNodes.item(i).getChildNodes().getLength() >= 1) { processChildNode(listOfNodes.item(i).getChildNodes(), tagPath, curLevel); } } else if(listOfNodes.item(i) instanceof Text && listOfNodes.getLength() == 1) { String value = http://stackoverflow.com/questions/11182420/listOfNodes.item(i).getNodeValue(); valueMap.put(tagPath, value); } } }}\[/code\]Here is the current output :\[code\]RootNode.Node_15.Node_25.Node_251 = Value_251RootNode.Node_14.Node_121.Node_1231.Node_12341 = LEVEL_4-Value_1RootNode.Node_12 = LEVEL_1-Value_2RootNode.Node_15 = Value_258RootNode.Node_14.Node_121.Node_1231 = LEVEL_4-Value_3RootNode.Node_15.Node_25 = Value_252RootNode.Node_11 = LEVEL_1-Value_1RootNode.Node_15.Node_251 = Value_257RootNode.Node_13.Node_121 = LEVEL_2-Value_1\[/code\]Please help me to make it work.Thanks.
 
Back
Top