0% found this document useful (0 votes)
18 views

Import Import Import Import Import Import Import Import Import Import Import Import Public Class Public Null

This Java class provides methods to parse XML documents into a DOM representation and extract element values. The getDocument method parses an XML string into a Document object, catching any parsing exceptions. The getValue method finds an element's text value by its name. The getTextNodeValue helper method retrieves the text content from a DOM Node.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Import Import Import Import Import Import Import Import Import Import Import Import Public Class Public Null

This Java class provides methods to parse XML documents into a DOM representation and extract element values. The getDocument method parses an XML string into a Document object, catching any parsing exceptions. The getValue method finds an element's text value by its name. The getTextNodeValue helper method retrieves the text content from a DOM Node.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

import android.util.

Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import
javax.xml.parsers.ParserConfigurationException;
public class XMLDOMParser {
public Document getDocument(String xml)
{
Document document = null;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db =
factory.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new
StringReader(xml));
is.setEncoding("UTF-8");
document = db.parse(is);
}catch(ParserConfigurationException e)
{
Log.e("Error: ", e.getMessage(), e);
return null;
}
catch (SAXException e) {
Log.e("Error: ", e.getMessage(), e);

return null;
}
catch(IOException e){
Log.e("Error: ", e.getMessage(), e);
return null;
}
return document;
}
public String getValue(Element item, String
name)
{
NodeList nodes =
item.getElementsByTagName(name);
return
this.getTextNodeValue(nodes.item(0));
}
private final String getTextNodeValue(Node
elem) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child =
elem.getFirstChild(); child != null; child =
child.getNextSibling() ){
if( child.getNodeType() ==
Node.TEXT_NODE ){
return
child.getNodeValue();
}
}
}
}
return "";
}

You might also like