I have huge number of XML files for which parsing needs to be done and generate a tree structure and further read to move data into database. Following is the structure which i think suffices my requirement. TreeNode class will have tag name, its properties and its parent tag (Tags here are xml elements)
public class TreeNode {
String tagname;
Map<String, String> tagmap;
TreeNode parent;
List<TreeNode> children;
public TreeNode(String tagname, Map<String, String> tagmap) {
this.tagname = tagname;
this.tagmap = tagmap;
this.children = new LinkedList<TreeNode>();
}
public TreeNode addChild(String tagname,Map<String, String> tagmap) {
TreeNode childNode = new TreeNode(tagname,tagmap);
childNode.parent = this;
this.children.add(childNode);
return childNode;
}
Now I am finding it difficult to parse XML file. Sample XML file is below
<head>
<link href="files/style.css" rel="stylesheet"/>
</head>
<div class="toolbar" style="display:block;position:absolute;top:0;left:0;width:100%;height:100%">
<TABLE datatable="0" summary="">
<prj:if condition="Platform">
<tr>
<td nowrap><prj:toolbar name="First RunningTest"><prj:running property="HTML"/></prj:toolbar></td>
<td> </td>
</tr>
<tr>
<td nowrap><prj:toolbar name="Second RunningTest"><prj:running property="HTML"/></prj:toolbar>
<td> </td>
</tr>
<tr>
<td nowrap><prj:toolbar name="Third RunningTest"><prj:running property="HTML"/></prj:toolbar>
<td> </td>
</tr>
</prj:if>
</TABLE>
</div>
Apart from HTML tags, prj tags are there project specific.
So the tree structure would be
root
--head (its map will be empty)
--link (its map will all its properties i.e. key,value pair)
--div
--table
--prj_if
--tr
--td
--prj_toolbar
--prj_running
--td
--tr
--td
--prj_toolbar
--prj_running
--td
--tr
--td
--prj_toolbar
--prj_running
--td
XML can contain any element(there could be hundreds of more prj tags apart from HTML tags). So i need to store all tags,its prop/value pair and its child node details. How do i read XML and move into Treemap. Also, once populated how to traverse the treemap to read its maps and its child elements map data.
please help in parsing XML
No comments:
Post a Comment