Java tutorial
//package com.java2s; /* * Keystone Development Framework * Copyright (C) 2004-2009 Rick Knowles * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * Version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License Version 2 for more details. * * You should have received a copy of the GNU Library General Public License * Version 2 along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Searches the node for content of type Long. If non-long content is found, * it logs a warning and returns null. */ public static Boolean extractBooleanFromElement(Node element) { String value = extractStringFromElement(element); return (value == null) ? null : new Boolean(value); } /** * Searches the node for content of type Long. If non-long content is found, * it logs a warning and returns null. */ public static String extractStringFromElement(Node element) { if (element == null) { return null; } else if (element.getFirstChild() == null) { return null; } else if (element.getFirstChild().getNodeValue() == null) { return null; } else { // Get all the children NodeList children = element.getChildNodes(); StringBuffer output = new StringBuffer(); for (int n = 0; n < children.getLength(); n++) { Node child = children.item(n); if (child.getNodeType() == Node.ENTITY_REFERENCE_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.ENTITY_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.TEXT_NODE) { output.append(child.getNodeValue()); } } return output.toString().trim(); } } }