Электронный магазин на Java и XML

         

Метод extractPrice (Product.java)



Листинг 6.21. Метод extractPrice (Product.java)

private void extractPrice( Element productElement ) { String s = Util.extractTextFrom( "price", productElement );
NumberFormat nf = NumberFormat.getCurrencyInstance();
try { Number n = nf.parse( s );
price = n.doubleValue();
} catch( ParseException pe ) { price = 0; } NodeList priceNodes = productElement.getElementsByTagName( "price" );
if( priceNodes.getLength() >
0 ) { Element price = (Element)priceNodes.item(0);
if( price != null ){ String d = price.getAttribute( "discount" );
if( d != null && !d.equals( "" ) ) { discount = new Integer( d );
} } } }

Извлечь информацию из элемента quantity_in_stock сравнительно легко. Используется знакомый нам метод extractTextFrom для получения строки, которая затем анализируется и преобразуется к типу i nt, как показано в листинге 6.22.



Содержание раздела