Java Reference
In-Depth Information
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
NodeList books = (NodeList) xp.evaluate("/book",
dom.getNode(),
XPathCon-
stants.NODESET);
String isbn = xp.evaluate("@isbn", books.item(0));
if (!library.containsKey(isbn))
throw new HTTPException(400);
String
pubYear
=
xp.evaluate("@pubyear",
books.item(0));
String
title
=
xp.evaluate("title",
books.item(0)).trim();
String
publisher
=
xp.evaluate("publisher",
books.item(0)).trim();
NodeList
authors
=
(NodeList)
xp.evaluate("author", books.item(0),
XPathCon-
stants.NODESET);
List<Author> auths = new ArrayList<>();
for (int i = 0; i < authors.getLength(); i++)
auths.add(new
Author(authors.item(i).getFirstChild()
.getNodeValue().trim()));
Book book = new Book(isbn, title, publisher, pub-
Year, auths);
library.put(isbn, book);
serialize();
}
catch (IOException | TransformerException e)
{
throw new HTTPException(500);
}
catch (XPathExpressionException xpee)
{
throw new HTTPException(400);
Search WWH ::




Custom Search