2013-03-12 142 views
0

我有一個XML名稱1.xml讀/寫,並創建本地XML文件

<?xml version="1.0" encoding="utf-8"?> 
<domains> 
<domain url="www.google.com" id="123"/> 
<domain url="www.yahoo.com" id="123"/></domains> 

現在我想讀這xml文件。我已將這個xml放在res>xml文件夾中。我怎樣才能讀這個XML文件?另外我想在這個XML中添加新的一些新的URL?所以有可能以編程方式?

+0

字符串jsPath = 「1.XML」 給出InputStream input = getClass()。getResourceAsStream(jsPath); byte [] content = IOUtilities.streamToBytes(input); String contentAsString = new String(content); – Signare 2013-03-12 12:55:41

+0

@Signare ok謝謝。我可以在這個XML中添加更多的網址嗎? – Hitarth 2013-03-12 12:58:13

+0

之前,將xml文件放在src/res /文件夾 – Signare 2013-03-12 12:58:51

回答

1

對於res文件夾從XML文件中讀取的值,使用以下代碼 -

try { 
    String jsPath = "1.xml"; 
    InputStream input = getClass().getResourceAsStream(jsPath); 
    byte [] content = IOUtilities.streamToBytes(input); 
    String contentAsString = new String(content); 
    //Dialog.alert(contentAsString); 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    XMLDOMUtil xml = new XMLDOMUtil(); 
    ByteArrayInputStream bis = new ByteArrayInputStream(contentAsString.getBytes("UTF-8")); 
    Document document = builder.parse(bis); 
    NodeList listOfPersons = document.getElementsByTagName("domains"); 
    NodeList listOfPersons1 = document.getElementsByTagName("domain"); 
    //int totalPersons = listOfPersons1.getLength(); 
    // Dialog.alert(totalPersons+""); 
    for(int s=0; s<listOfPersons1.getLength() ; s++) 
     { 
     Node firstPersonNode = listOfPersons1.item(s); 
     Element firstPersonElement = (Element)firstPersonNode; 
     Dialog.alert(firstPersonElement.getAttribute("url")+"---"+firstPersonElement.getAttribute("id")); 
     } 

} catch (IOException e) { 
    e.printStackTrace(); 
} catch (SAXException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (ParserConfigurationException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

XMLDOMUtil.java被如下─

public class XMLDOMUtil { 
     // go thru the list of childs and find the text associated by the tag 
     public String getNodeTextByTag(Node parentNode, String name) { 
       Node node = parentNode.getFirstChild(); 
       Text text = null; 
       String retStr = null; 
       try { 
         while (node != null) { 
           if (node.getNodeName().equals(name)) { 
             text = (Text) node.getFirstChild(); 
             retStr = text.getData(); 
             break; 
           } 
           node = node.getNextSibling(); 
         } 
       } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
       } 
       return retStr; 
     } 
     public Node getNodeByTag(Node parentNode, String name) { 
       Node node = parentNode.getFirstChild(); 
       Node retNode = null; 
       while (node != null) { 
         if (node.getNodeName().equals(name)) { 
           retNode = node; 
           break; 
         } 
         node = node.getNextSibling(); 
       } 
       return retNode; 
     } 
     public Node getNextSiblingNodeByTag(Node siblingNode, String name) { 
       Node retNode = null; 
       siblingNode = siblingNode.getNextSibling(); 
       while (siblingNode != null) { 
         if (siblingNode.getNodeName().equals(name)) { 
           retNode = siblingNode; 
           break; 
         } 
         siblingNode = siblingNode.getNextSibling(); 
       } 
       return retNode; 
     } 
} 
相關問題