aspose file tools*
The moose likes XML and Related Technologies and the fly likes problem with parsing xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "problem with parsing xml file " Watch "problem with parsing xml file " New topic
Author

problem with parsing xml file

pvsr rao
Ranch Hand

Joined: Oct 05, 2005
Posts: 102
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
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
Are you sure that "l.item(2)" returns an element? Maybe the index is off and it's returning something else?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with parsing xml file