2011年8月30日 星期二

[Android] How to read and parse xml files in android application.

I have a xml file named books.xml that is stored in my pad.

<?xml version="1.0" encoding="UTF-8"?>
<library-metadata title="My library">
  <book-metadata name="One Day" author="Nicholls, David">
    <publish-info publisher="Random House Inc" date="05-24-2011" ISBN="9780307946713"/>
  </book-metadata>
  <book-metadata name="Transformers: Exodus" author="Irvine, Alex">
    <publish-info publisher="Random House" date="06-28-2011" ISBN="9780345522528"/>
  </book-metadata>
</book-metadata>

I want to read the xml content and save it to my custom class.
The code as below.


        final String path = Environment.getExternalStorageDirectory().getPath() + "/books.xml";
        FileInputStream fstream = null;
        try
        {
            // read file.
            fstream = new FileInputStream(path);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(fstream);
            Element root = document.getDocumentElement();


            NodeList bookList = root.getElementsByTagName("book-metadata");
            for (int i = 0; i < bookList.getLength(); i++)
            {
                Element bookElement = (Element) bookList.item(i);


                NodeList publishList = bookElement.getElementsByTagName("publish-info");
                for (int j = 0; j < publishList.getLength(); j++)
                {
                    classBooks myBook = new classBooks();
                    Element publishElement = (Element) publishList.item(j);


                    // get xml value and set it into class object.
                    myBook.setName(Integer.parseInt(bookElement.getAttribute("name")));
                    myBook.setAuthor(bookElement.getAttribute("author"));
                    myBook.setPublisher(publishElement.getAttribute("publisher"));
                    myBook.setPublishDate(publishElement.getAttribute("date"));
                    myBook.setISBN(publishElement.getAttribute("ISBN"));


                    m_bookList.add(myBook);  // m_bookList is a member collection variable.
                }
            }
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
        finally
        {
            if (null != fstream)
            {
                fstream.close();
            }
        }

2011年8月2日 星期二

[.Net] Can't find PInvoke DLL 'sqlceme35.dll

You need to install Compact on the device if you met the problem.


1. Copy the file sqlce.ppc.wce5.armv4i.CAB in the path 
C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices\wce500\armv4i into device or emulator


2. execute it on device.




Reference:
http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/8faa4e72-2dfe-44d4-b8b1-8fd64df01e7d/






[.Net] Emulate a storage card using folder sharing

We hope to simulate a storage card insert into Microsoft Device Emulator.
We have to create a folder shared to the emulator, it appears as a storage card.
You can also stop folder sharing when you no longer want to work with an emulated storage card.


To activate folder sharing

  1. On the Device Emulator File menu, click Configure.
  2. Select the General tab.
  3. In the Shared folder box, type the path and folder name for the directory you want to use as an emulated storage card. (also can click the Browse button, navigate to the directory that you want to use.)
  4. Click OK.


Reference:
http://msdn.microsoft.com/en-us/library/aa188184(v=vs.90).aspx