前几天单词app中,已经可以显示单词了,对比有道词典的单词表,主要是有单词备注的功能,可以进行联想记忆,而且程序占用空间小,没有太多的通知信息。
以前使用有道词典的单词保存了一些单词,有道里的单词可以导出到xml文件中
格式如下
<wordbook><item> <word>unveil</word>
<trans><![CDATA[vt. 使公诸于众,揭开;揭幕
vi. 除去面纱;显露
unveil: 揭露 | 使公诸于众 | 揭开]]></trans>
<phonetic><![CDATA[[ʌn'veɪl]]]></phonetic>
<tags></tags>
<progress>2</progress>
</item><item> <word>prominence</word>
<trans><![CDATA[n. 突出;显著;突出物;卓越
Prominence: 日珥 | 突出 | 显着]]></trans>
<phonetic><![CDATA[['prɒmɪnəns]]]></phonetic>
<tags></tags>
<progress>1</progress>
</item></wordbook>
考虑增加导入功能,读取单词xml获取数据保存进去
在java上进行xml解析练习
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Test {
public static void getDataFromXml(String fileName)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(fileName);
NodeList wordList = doc.getElementsByTagName("item");
System.out.println("共有" + wordList.getLength() + "个word节点");
for (int i = 0; i < wordList.getLength(); i++)
{
if (i == 3)
{
break;
}
Node aWord = wordList.item(i);
Element elem = (Element) aWord;
for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
{
// System.out.println("node.getNodeType() = " + node.getNodeType());
if (node.getNodeType() == Node.ELEMENT_NODE)
{
String name = node.getNodeName();
if (name.equals("word") || name.equals("trans"))
{
String value = node.getFirstChild().getNodeValue();
System.out.print(name + ":" + value + "\t");
}
}
}
System.out.println();
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("----------");
getDataFromXml("D:\\2015\\06\\words.xml");
}
}
把解析方法移植到单词app中,出现错误, MalformedURLException: Protocol not found
解决方案,路径修改为 file:///mnt/sdcard/words.xml
即增加file://
/mnt/sdcard/words.xml 为xml文件保存的地方,即手机上打开 文件管理 所在的路径
//
public void getDataFromXml(String fileName)
{
Log.d("test614", fileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(fileName);
NodeList wordList = doc.getElementsByTagName("item");
System.out.println("共有" + wordList.getLength() + "个word节点");
Log.d("test614", "共有" + wordList.getLength() + "个word节点");
for (int i = 0; i < wordList.getLength(); i++)
{
Node aWord = wordList.item(i);
Element elem = (Element) aWord;
String wname = " ";
String wtrans = " ";
for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
{
if (node.getNodeType() == Node.ELEMENT_NODE)
{
String name = node.getNodeName();
if (name.equals("word"))
{
wname = node.getFirstChild().getNodeValue();
}
if (name.equals("trans"))
{
wtrans = node.getFirstChild().getNodeValue();
}
}
}
if (!wname.equals(" "))
{
this.insertStuInfo(wname, wtrans, " ");
}
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
Log.d("test614", e.toString());
}
}
调用
getDataFromXml("file:///mnt/sdcard/words.xml");
//insert a record
//6-21
private void insertStuInfo(String word, String trans, String notes)
{
ContentValues values = new ContentValues();
values.put(StuInfoColumns.WORD_NAME, word);
values.put(StuInfoColumns.WORD_MEANING, trans);
values.put(StuInfoColumns.WORD_NOTES, notes);
getContentResolver().insert(StuInfoColumns.CONTENT_URI, values);
}
根据字母表排序的单词对出现大写字母优先于小写字母的现象,可以使用upper对排序条件进行修饰
增加解析tags内容后,发现如果内容为空,会出错
node.getFirstChild().getNodeValue()
//will lead a nullPointer exception
解析方法的优化:
//
public void getDataFromXml(String fileName)
{
Log.d("test614", fileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(fileName);
NodeList wordList = doc.getElementsByTagName("item");
System.out.println("共有" + wordList.getLength() + "个word节点");
Log.d("test614", "共有" + wordList.getLength() + "个word节点");
for (int i = 0; i < wordList.getLength(); i++)
{
Node aWord = wordList.item(i);
Element elem = (Element) aWord;
String wname = " ";
String wtrans = " ";
String wnotes = " ";
for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
{
if (node.getNodeType() == Node.ELEMENT_NODE)
{
//if there is no value, continue
//otherwise node.getFirstChild().getNodeValue()
//will lead a nullPointer exception
if (node.getFirstChild() == null)
{
continue;
}
String name = node.getNodeName();
if (name.equals("word") )
{
wname = node.getFirstChild().getNodeValue();
Log.d("test614", "word=" + wname);
}
else if (name.equals("trans"))
{
//node.getNodeValue()
wtrans = node.getFirstChild().getNodeValue();
Log.d("test614", "trans=" + wtrans);
}
else if (name.equals("tags"))
{
wnotes = node.getFirstChild().getNodeValue();
if (wnotes == null)
{
Log.d("test614", "tags=null");
}
else
{
Log.d("test614", "tags=" + wnotes);
}
}
}
}
if (wname!= null && !wname.equals(" "))
{
Log.d("test614", "if (!wname.equals() " + wname);
this.insertStuInfo(wname, wtrans, wnotes);
}
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
Log.d("test614", e.toString());
}
}