rubal18 / google-checkout-java-sample-code

Automatically exported from code.google.com/p/google-checkout-java-sample-code
1 stars 0 forks source link

Utils.getElements() throws Exception when not all subChildren are Elements #5

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. While processing a checkout-shopping-cart XML notification
2.
Element shoppingCartElement = Utils.findElementOrContainer(document, 
document.getDocumentElement(), "shopping-cart");
Element itemsElement = Utils.findElementOrContainer(document, 
shoppingCartElement, "items");
Element[] childElements = Utils.findElements(document,itemsElement);

What is the expected output? What do you see instead?

Attempted to get item list from checkout-shopping-cart notification, but 
Utils.getElements() throws class cast exception on non-element items.

What version of the product are you using? On what operating system?

Using version JavaCheckoutRefImpl-0.2
Java 1.6 on Windows Vista

Please provide any additional information below.

See d.patterson comment on http://forum.java.sun.com/thread.jspa?
threadID=226630&messageID=4359876

Fix:

NodeList nl = itemsElement.getChildNodes();
ArrayList<Element> childNodes = new ArrayList<Element>();
   for(int i=0; i<nl.getLength(); i++ ) {
      Node node = nl.item(i);
      if(node instanceof Element) { // need to do test to see if just 
string with return character
      Element test = (Element)node;
      childNodes.add(test);
   }
}
Element[] elements = childNodes.toArray(new Element[childNodes.size()]);

Original issue reported on code.google.com by rob.broa...@gmail.com on 27 Apr 2007 at 12:36

GoogleCodeExporter commented 9 years ago
I ran into this as well.

I prefer the fix 

if (node.getNodeType() == Node.ELEMENT)
{
  // add element
}

but the above is probably better.

Original comment by will.sar...@gmail.com on 30 Apr 2007 at 8:21