DOM Java HTML Parser (Replace all occurances)


Joined
Apr 1, 2012
Messages
1
Reaction score
0
I want to replace all occurrances of an old tag in a DOM tree with a new tag. But it keeps giving me error red highlighted on .text, and .size() so is there any other way to fix it? Your help will greatly be appreciated.

Code:

public void replaceTag(String oldTag, String newTag)
{
ArrayList<String> tags = new ArrayList<String>(); //initialize an ArrayList so we can store open tags

replaceTag(root, oldTag, newTag, tags); //call recursive method
}

//helper method for replacing tags
private void replaceTag(TagNode r, String oldTag, String newTag, ArrayList<String> tagList)
{

if(r.firstChild.equals(0)) //if no children, return
{
return;
}

else
{
//if it has children, it is a tag,
tagList.add(r.text); //add the tag to the open tags ArrayList
if(r.text.equals(oldTag)) //check to see if this node's tag matches one to remove
{
if(newTag.equals("ol") || newTag.equals("ul")) //if inserting an ol or ul tag, we know these can be nested within each other
{
r.text = newTag;
}
else //otherwise make sure the tag inserting is not nested within same family
{
boolean notNestedAbove = true; //initialize a boolean which will be flagged false if same tag is found in open tags ArrayList
for(int i = 0; i < tagList.size(); i++) //loop through ArrayList of current open tags
{
if(tagList.get(i).equals(newTag))
}
}

boolean b = true;
{
notNestedAbove = false; //can't insert nested like tag
break;
boolean notNestedBelow = isUnique(r,newTag, b);

if(notNestedAbove && notNestedBelow)
{
r.text = newTag; //tag that will be uniquely nested, replace tags
}
else
{
System.out.println("Duplicate tags may not be nested.");
return;
}
}
}

for(int i = 0; i<r.firstChild.size(); i++)
{
replaceTag(r.firstChild, oldTag, newTag, tagList); //then recurse on children
}
tagList.remove(tagList.size()-1); //remove the open tag from the end of the ArrayList
}
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top