Java Reference
In-Depth Information
for (int i = 0; i < count; i++) {
Question question = new Question();
Element questionNode = (Element) questionNodes.item(i);
NodeList childNodes = questionNode.getChildNodes();
int cnt2 = childNodes.getLength();
for (int j = 0; j < cnt2; j++) {
Node me = childNodes.item(j);
String nodeName = me.getNodeName();
if ("creation_date".equals(nodeName)) {
question.setTimestamp(Long.parseLong(me.getTextContent()));
}
if ("owner".equals(nodeName)) {
question.setOwner(me.getTextContent());
}
if ("title".equals(nodeName)) {
question.setQuestion(me.getTextContent());
}
}
answer.add(question);
}
return answer;
}
Again, the goal of this section is not to give a comprehensive overview of the DOM APIs. There are a large number
of resources available on the Internet that provide information about XML in general, or DOM in particular.
To be able to compile the code in Listing 11-8, the following import statements had to be added.
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
Before we go into detail about the code, we show the output of this example in Figure 11-6 .
 
Search WWH ::




Custom Search