读/写并创建本地xml文件

时间:2013-03-12 12:52:18

标签: xml blackberry file-io read-write

我有一个xml这样的名字1.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?它可以以编程方式吗?

1 个答案:

答案 0 :(得分: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;
        }
}