InputStream in=getResources().openRawResource(R.raw.book);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(in);
Log.i(TAG," doc NodeType "+doc.getNodeType());
NodeList bookList=doc.getElementsByTagName("book");
for (int i=0;i<bookList.getLength();i++){
Node book=bookList.item(i);
if (book.getNodeType()==Node.ELEMENT_NODE){
Log.i(TAG,"-----------book-------------");
Element bookElement=(Element)book;
count++;
Log.i(TAG,"count :"+count+" "+bookElement.getAttribute("id"));
Element nameEle=(Element)bookElement.getElementsByTagName("name").item(0);
Log.i(TAG,"name NodeType "+nameEle.getNodeType());
Log.i(TAG," ss : "+nameEle.getAttribute("ss"));
Log.i(TAG," name: "+nameEle.getTextContent());
NodeList authorList=bookElement.getElementsByTagName("author");
for (int j=0;j<authorList.getLength();j++){
Node authorNode=authorList.item(j);
Log.i(TAG,"NodeType : "+authorNode.getNodeType());
Element authorEle=(Element)authorNode;
Log.i(TAG," author "+j+" :"+authorEle.getTextContent());
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
遍历整个树
public void loopXml() {
try{
InputStream in=getResources().openRawResource(R.raw.book);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(in);
if (doc.hasChildNodes()){
printNode(doc.getChildNodes());
}
} catch(Exception e){
e.printStackTrace();
}
}
public void printNode(NodeList nodeList){
for (int i=0;i<nodeList.getLength();i++){
Node node=nodeList.item(i);
if (node.getNodeType()==Node.ELEMENT_NODE){
Log.i(TAG,"<"+node.getNodeName()+">");
if (node.hasAttributes()){
NamedNodeMap nodeMap=node.getAttributes();
for (int j=0;j<nodeMap.getLength();j++){
Node no=nodeMap.item(j);
Log.i(TAG, " "+no.getNodeName()+" "+no.getNodeValue());
}
}
if (node.hasChildNodes()){
printNode(node.getChildNodes());
} else {
Log.i(TAG," "+node.getTextContent());
}
Log.i(TAG,"<"+node.getNodeName()+"/>");
} else {
if (node.getNodeType()==Node.TEXT_NODE){
Text text=(Text)node;
Log.i(TAG," "+text.getTextContent());
}
// Log.i(TAG,"nodetype "+node.getNodeType()+" not Ele :"+node.getNodeName()+" "+node.getNodeValue());
}
}
}
DOM方式下所有内容都是存在节点Node中的,
<name>godlike</name>
节点的类型是Node.ELEMENT_NODE,而godlike
的类型是TEXT_NODE。
生成XML文件
Mode类
public class Mode {
String id;
String reason;
List<String> thought;
Mode(){
}
Mode(int n,String... strings){
thought=Arrays.asList(strings);
}
public List<String> getTh(){
return thought;
}
public void setId(String id){
this.id=id;
}
public String getReason(){
return reason;
}
public String getId(){
return id;
}
public void setReason(String reason){
this.reason=reason;
}
public void f(int ...f){
}
}
初始化
Mode mode=new Mode(3,"lirui","hao","fan");
mode.setId("1");
mode.setReason("nonono");
list.add(mode);
mode=new Mode(1,"xxx");
mode.setId("2");
mode.setReason("sikao");
list.add(mode);
createXml(list);
主要代码
public void createXml(List<Mode> list){
Document doc;
Element modes;
Element mode;
Element reason;
Element thought;
try{
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=dbFactory.newDocumentBuilder();
doc=builder.newDocument();
if (doc!=null){
modes=doc.createElement("modes");
for (int i=0;i<list.size();i++){
mode=doc.createElement("mode");
mode.setAttribute("id", list.get(i).getId());
reason=doc.createElement("reason");
reason.appendChild(doc.createTextNode(list.get(i).getReason()));
mode.appendChild(reason);
List<String> strs=list.get(i).getTh();
for (int j=0;j<strs.size();j++){
thought=doc.createElement("thought");
thought.appendChild(doc.createTextNode(strs.get(j)));
mode.appendChild(thought);
}
modes.appendChild(mode);
}
doc.appendChild(modes);
TransformerFactory transF=TransformerFactory.newInstance();
Transformer t=transF.newTransformer();
DOMSource source=new DOMSource(doc);
Log.i(TAG,"thi OKKKK");
Log.i(TAG,Environment.getExternalStorageState());
Log.i(TAG,Environment.getExternalStorageDirectory().getPath()+"/xx.xml");
StreamResult result=new StreamResult(Environment.getExternalStorageDirectory().getPath()+"/xx.xml");
t.transform(source, result);
Log.i(TAG,"OKKKK");
}
} catch(Exception e){
e.printStackTrace();
Log.i(TAG,"NOT OKKKK");
Log.i(TAG,e.getMessage());
}
}
几点问题:
- 如何获得外挂存储卡的位置
Environment.getExternalStorageDirectory().getPath()+"/xx.xml"
在小米手机上这是手机自带的存储卡的位置,不是SD卡。要想读写存储卡还需要权限<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> - 可变参数是数组。