Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] remove all comments

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: luca <passani AT eunet.no>
  • To: xom-interest <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] remove all comments
  • Date: Thu, 11 Nov 2004 14:04:27 +0100



Elliotte Harold wrote:

Do you really want to traverse it? Why not just build the document without comments by using a custom node factory that never includes comments in the first place?

I have done this already (code at the end for the record).
My problem is that I am acting on an already-parsed WURFL.xml.
My understanding is that parsing from scratch wouldn't help
me much. Not only do I need to remove the comments (that's
second priority actually), but I need to remove all the capabilities
that are not in a given list and, in addition, I need to make sure that
there are no empty groups left.

The whole idea is to have a filter that lets people create
a mini-wurfl containing only capabilities relevant for that user.
Here is the first prototype:

http://demo.openwave.com:8080/wurfltests/wurflfilter.jsp

Luca

public String filterCapabilities(HashSet capaAcceptedList) {

if (wurflDocument == null) {
return "";
}
Document tmpWurflDocument = (Document) wurflDocument.copy();
//cleaning. remove version element
Element version =
tmpWurflDocument.getRootElement().getFirstChildElement("version");
if (version != null)
{tmpWurflDocument.getRootElement().removeChild(version);}


Element devices_elem =
tmpWurflDocument.getRootElement().getFirstChildElement("devices");

//more cleaning. remove all comments
for (int h = 0; h < devices_elem.getChildCount(); h++) {
Node child = devices_elem.getChild(h);
if (child instanceof Comment) {
devices_elem.removeChild(child);
}
}

Elements devices = devices_elem.getChildElements("device");
int numberOfDevices = devices.size();

for(int k = 0; k < devices.size(); k++) {
Element device = devices.get(k);
//some cleaning. remove all comments
for (int h = 0; h < device.getChildCount(); h++) {
Node child = device.getChild(h);
if (child instanceof Comment) {
device.removeChild(child);
}
}
Elements groups = device.getChildElements("group");

//extra list to keep capabilities grouped by group
for (int i = 0; i < groups.size(); i++) {
Element group = groups.get(i);

//some cleaning. remove all comments
for (int h = 0; h < group.getChildCount(); h++) {
Node child = group.getChild(h);
if (child instanceof Comment) {
group.removeChild(child);
}
}
Elements capaList = group.getChildElements("capability");
for (int j = 0; j < capaList.size(); j++) {
Element capa = capaList.get(j);
String capa_name = capa.getAttributeValue("name");
if (!capaAcceptedList.contains(capa_name)) {
//remove capability
group.removeChild(capa);
}
}
//if group has no capability, remove group too
if (0 == group.getChildElements("capability").size()) {
device.removeChild(group);
}
}
}
try {
return toPrettyXML(tmpWurflDocument);
} catch (Exception e) {
return "Error producing filtered WURFL:"+e.getMessage();
}
}






Archive powered by MHonArc 2.6.24.

Top of Page