[Simple-support] Best way to loop over elements
Brought to you by:
niallg
|
From: Jason R. <jr...@ri...> - 2011-01-21 15:25:51
|
I have an xml file that has this structure:
<Rhs>
<Ads>
<Zone name="default">
<Page name="default">
<Position position="1">
<adUrl>http://ad.server.net/adj/trm.ra/default;pos=RR1;sz=120x207;tile=1</adUrl>
</Position>
<Position position="2">
<adUrl>http://ad.server.net/adj/trm.ra/default;pos=RR2;sz=120x207;tile=2</adUrl>
</Position>
</Page>
</Zone>
</Ads>
</Rhs>
I have classes for Ads, Zones, Page and Position that are working fine for getting me the adUrl data for a particular page name. Now, in my code when I want to render certain code that uses the adUrl, I have the following, which seems to go over every element in the xml file, whereas I was wondering if I could break out of the loop once it finds the page I'm on and get's those values. The issue is that once I generate the code for the current page, it still goes through my else condition and generates all of the code for every other page in my xml file, which I don't want. Here is the code that does the looping:
StringBuffer rhsStringBuffer = new StringBuffer();
//Get the page for which the RHS should be displayed
rhsPage = getRightHandSideValue();
Ads ads = RhsHelperMethods.getRhsDetailsByPageName();
for (Zone zone : ads.getZones()) {
for (Page page : zone.getPages()) {
if (page.name.equalsIgnoreCase(rhsPage)) {
for (Position position : page.getPositions()) {
rhsStringBuffer.append("<script type=\"text/javascript\">\r\n");
rhsStringBuffer.append("ord = window.ord || Math.floor(Math.random()*1E16);\r\n");
rhsStringBuffer.append("document.write('<script type=\"text/javascript\" ");
rhsStringBuffer.append("src=\"");
rhsStringBuffer.append(position.getAdUrl());
rhsStringBuffer.append(";ord=' + ord + '?\">\r\n");
rhsStringBuffer.append("<\\/script>');\r\n");
rhsStringBuffer.append("</script>\r\n");
rhsStringBuffer.append("<noscript>\r\n");
rhsStringBuffer.append("<a href=\"");
rhsStringBuffer.append(position.getAdUrl());
rhsStringBuffer.append(";ord=123456789?\" target=\"_blank\">\r\n");
rhsStringBuffer.append("<img src=\"");
rhsStringBuffer.append(position.getAdUrl());
rhsStringBuffer.append(";ord=123456789?\" alt=\"NEED ALT TEXT HERE\" />\r\n");
rhsStringBuffer.append("</a>\r\n");
rhsStringBuffer.append("</noscript>\r\n");
}
} else {
page.setName("default");
for (Position position : page.getPositions()) {
logger.debug("Missing page name, using default RHS");
rhsStringBuffer.append("<script type=\"text/javascript\">\r\n");
rhsStringBuffer.append("ord = window.ord || Math.floor(Math.random()*1E16);\r\n");
rhsStringBuffer.append("document.write('<script type=\"text/javascript\" ");
rhsStringBuffer.append("src=\"");
rhsStringBuffer.append(position.getAdUrl());
rhsStringBuffer.append(";ord=' + ord + '?\">\r\n");
rhsStringBuffer.append("<\\/script>');\r\n");
rhsStringBuffer.append("</script>\r\n");
rhsStringBuffer.append("<noscript>\r\n");
rhsStringBuffer.append("<a href=\"");
rhsStringBuffer.append(position.getAdUrl());
rhsStringBuffer.append(";ord=123456789?\" target=\"_blank\">\r\n");
rhsStringBuffer.append("<img src=\"");
rhsStringBuffer.append(position.getAdUrl());
rhsStringBuffer.append(";ord=123456789?\" alt=\"NEED ALT TEXT HERE\" />\r\n");
rhsStringBuffer.append("</a>\r\n");
rhsStringBuffer.append("</noscript>\r\n");
}
}
}
}
rhsItem = rhsStringBuffer.toString();
Thanks
________________________________
Disclaimer: This e-mail message is intended only for the personal use of
the recipient(s) named above. If you are not an intended recipient, you
may not review, copy or distribute this message. If you have received this
communication in error, please notify us immediately by e-mail and delete
the original message.
This e-mail expresses views only of the sender, which are not to be
attributed to Rite Aid Corporation and may not be copied or distributed
without this statement.
|