Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with parsing xml file

 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in web.xml file we have filter tag
<filter>
<filter-name>filtername</filter-name>
<filter-class>filterclass</filter-class>
</filter>
in this i have to write programmitically

public void createFilter(String a,String b)
{
try
{
NodeList l = getRootNode().getChildNodes();
Node filterNode = l.item(2);as in web.xml second element is filter tag
String aa = "kk";
String bb = "kkk";
aa = a;
bb = b;
addFilter((Element)filterNode,a,b);
}catch(Exception e)
{
e.printStackTrace();
}
}




public void addFilter(Element filter,String a,String b)throws Exception
{
String name1="filterclass";
String name2="fill";
Element filtername=document.createElement("filter-name");
Element filterclass=document.createElement("filter-class");
Text spaceElement1 = document.createTextNode("\n");
Text spaceElement2 = document.createTextNode("\n");
Text spaceElement3 = document.createTextNode("\n");
Text spaceElement4 = document.createTextNode("\n");
name1=a;
name2=b;
Text filternameVal = document.createTextNode(name1);
Text filterclassVal = document.createTextNode(name2);
filtername.appendChild(filternameVal);
filterclass.appendChild(filterclassVal);
filter.appendChild(filtername);
filter.appendChild(spaceElement4);
filter.appendChild(filterclass);
filter.appendChild(spaceElement3);
}
iam getting classcast exception.
please reply me what the wrong i have done.
if iam not casting to Element it is displaying error message as Node not allowed here
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that "l.item(2)" returns an element? Maybe the index is off and it's returning something else?
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your comments suggest that you want to find the second element child of some node, but your code is actually finding the second node child instead. You forgot to take into account all those whitespace text nodes that are separating your tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic