Below code an be used to parse an XML string
See the sample XMl given below:
<entry>
<dc:creator>Jane Kirkland</dc:creator>
<dc:creator>Dorothy Burke</dc:creator>
<dc:date>2005-11-02</dc:date>
<dc:description>
An essential guide and reference tool to Lotus Notes 7 with practical answers for people who need fast results.
</dc:description>
<dc:format>366 pages</dc:format>
<dc:identifier>9FEdL2eH6t8C</dc:identifier>
<dc:identifier>ISBN:0672328003</dc:identifier>
<dc:identifier>ISBN:9780672328008</dc:identifier>
<dc:publisher>Sams Publishing</dc:publisher>
<dc:subject>Computers</dc:subject>
<dc:title>Sams Teach Yourself Lotus Notes 7 in 10 Minutes</dc:title>
</entry>
public static void parseME(String xmlRecords) {
try {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("entry");
NodeList name = null;
// iterate the entries
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;
NodeList nameNode = element.getElementsByTagName("dc:creator");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Name = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:date");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Date = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:description");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Description = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:format");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Format = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:identifier");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Identifier = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:publisher");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Publisher = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:subject");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Subject = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:title");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Title = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Thursday, February 19, 2009
Subscribe to:
Post Comments (Atom)
So.. This month's special is XML and parsing, huh??
ReplyDelete