在android中使用xml文件

时间:2022-12-01 11:23:42

I have an xml file

我有一个xml文件

How can i display elements from this file (XQUERY an XPATH) (getchild, getchildren) with android.

如何使用android显示此文件中的元素(XQUERY和XPATH)(getchild,getchildren)。

please explain me how can i use xml file with android.

请解释我如何使用Android的xml文件。

Thank you in advance.

先感谢您。

2 个解决方案

#1


0  

This does not work on an android project because of the missing libraries on the android SDK ,references like "import javax.xml.transform" wont work,please correct me if im making a mistake some where or if there is a way to reference it on android

这对Android项目不起作用,因为android SDK上缺少库,像“import javax.xml.transform”这样的引用不起作用,如果我在哪里或者如果有一种方法可以引用它,请纠正我在android上

#2


0  

This is a little class something I use to generate a xml document element in Android. It only uses a DOM parser which can use a lot of memory.

这是我用来在Android中生成xml文档元素的一个小类。它只使用可以使用大量内存的DOM解析器。

import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Some general XML functions
 */
public class XML
{
    /**
     * Get an XML Document file from its string representation
     * @param xml The xml string
     * @return Document object for the XML representation
     */
    public static Document fromString(String xml)
    {
        if (xml == null)
            throw new NullPointerException("The xml string passed in is null");

        // from http://www.rgagnon.com/javadetails/java-0573.html
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));

            Document doc = db.parse(is);

            return doc;
        }
        catch (SAXException e)
        {
            return null;
        }
        catch(Exception e)
        {
            CustomExceptionHandler han = new CustomExceptionHandler();
            han.uncaughtException(Thread.currentThread(), e);
            return null;
        }
    }

    /**
     * Get a XML Document object from a file
     * @param location The location where the file is found
     * @return An XML Document object
     */
    public static Document fromFile(String location)
    {
        return fromFile(new File(location));
    }

    /**
     * Get a XML Document object from a file
     * @param location The file object to load
     * @return An XML Document object
     */
    public static Document fromFile(File file)
    {
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            return doc;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    /**
     * Method to convert Document to String
     * @param doc
     * @return
     */
    public static String getStringFromDocument(Document doc)
    {
        try
        {
           DOMSource domSource = new DOMSource(doc);
           StringWriter writer = new StringWriter();
           StreamResult result = new StreamResult(writer);
           TransformerFactory tf = TransformerFactory.newInstance();
           Transformer transformer = tf.newTransformer();
           transformer.transform(domSource, result);
           return writer.toString();
        }
        catch(TransformerException ex)
        {
           ex.printStackTrace();
           return null;
        }
    }
}

Then to use the Document object returned, use its functions such as getAttribute or getElementsByTagName etc. Its quite easy to use

然后使用返回的Document对象,使用其函数,如getAttribute或getElementsByTagName等。它很容易使用

#1


0  

This does not work on an android project because of the missing libraries on the android SDK ,references like "import javax.xml.transform" wont work,please correct me if im making a mistake some where or if there is a way to reference it on android

这对Android项目不起作用,因为android SDK上缺少库,像“import javax.xml.transform”这样的引用不起作用,如果我在哪里或者如果有一种方法可以引用它,请纠正我在android上

#2


0  

This is a little class something I use to generate a xml document element in Android. It only uses a DOM parser which can use a lot of memory.

这是我用来在Android中生成xml文档元素的一个小类。它只使用可以使用大量内存的DOM解析器。

import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Some general XML functions
 */
public class XML
{
    /**
     * Get an XML Document file from its string representation
     * @param xml The xml string
     * @return Document object for the XML representation
     */
    public static Document fromString(String xml)
    {
        if (xml == null)
            throw new NullPointerException("The xml string passed in is null");

        // from http://www.rgagnon.com/javadetails/java-0573.html
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));

            Document doc = db.parse(is);

            return doc;
        }
        catch (SAXException e)
        {
            return null;
        }
        catch(Exception e)
        {
            CustomExceptionHandler han = new CustomExceptionHandler();
            han.uncaughtException(Thread.currentThread(), e);
            return null;
        }
    }

    /**
     * Get a XML Document object from a file
     * @param location The location where the file is found
     * @return An XML Document object
     */
    public static Document fromFile(String location)
    {
        return fromFile(new File(location));
    }

    /**
     * Get a XML Document object from a file
     * @param location The file object to load
     * @return An XML Document object
     */
    public static Document fromFile(File file)
    {
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            return doc;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    /**
     * Method to convert Document to String
     * @param doc
     * @return
     */
    public static String getStringFromDocument(Document doc)
    {
        try
        {
           DOMSource domSource = new DOMSource(doc);
           StringWriter writer = new StringWriter();
           StreamResult result = new StreamResult(writer);
           TransformerFactory tf = TransformerFactory.newInstance();
           Transformer transformer = tf.newTransformer();
           transformer.transform(domSource, result);
           return writer.toString();
        }
        catch(TransformerException ex)
        {
           ex.printStackTrace();
           return null;
        }
    }
}

Then to use the Document object returned, use its functions such as getAttribute or getElementsByTagName etc. Its quite easy to use

然后使用返回的Document对象,使用其函数,如getAttribute或getElementsByTagName等。它很容易使用