read/write and create local xml file

前端 未结 1 1971
北恋
北恋 2021-01-27 13:45

i have one xml name 1.xmllike that





        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-27 14:05

    For reading values from XML file in res folder, Use the following code-

    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

    XMLDOMUtil.java is given below-

    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;
            }
    }
    

    0 讨论(0)
提交回复
热议问题