【问题标题】:How to read the child nodes from XML using DOM or SAX parser in android如何在 android 中使用 DOM 或 SAX 解析器从 XML 读取子节点
【发布时间】:2023-03-10 19:34:01
【问题描述】:

这是我的 XML。

<Operations>
<Operation Name="OperationName1">Entity details1</Operation>
<Operation Name="OperationName2">Entity details2</Operation>
<Operation Name="OperationName3">Entity details3</Operation>
<Operation Name="OperationName4">Entity details4</Operation>
</Operations>

在此我需要将每个子节点作为字符串变量读取。使用 DOM 我正在​​尝试这样。

NodeList items = root.getElementsByTagName("Operation");

        for (int i=0;i<items.getLength();i++)
        {   
            Node item = items.item(i);

            NodeList properties = item.getChildNodes();

            for (int j=0;j<properties.getLength();j++){

                Node property = properties.item(j);

                    }               
         }

现在据我所知,这些项目现在拥有所有子节点,我需要像这样存储每个子节点。

String ch_node="<Operation Name="OperationName4">Entity details4</Operation>"

是否有任何默认方法可以为我提供子节点 xml,或者我需要使用节点名称、值和属性再次创建?

我也尝试过使用 SAX 解析器,但不知道如何获取。

public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {       
    if (qName.equalsIgnoreCase("operation")) {     
        op_Name=attributes.getValue(0);
    }
}

public void characters(char[] ch, int start, int length)
        throws SAXException {        
}

public void endElement(String uri, String localName, String qName)
        throws SAXException {       
}

【问题讨论】:

    标签: java android xml-parsing saxparser domparser


    【解决方案1】:

    你可以试试 DOM 和 Transformer

        Transformer tx = TransformerFactory.newInstance().newTransformer();
        tx.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("1.xml"));
        NodeList list = doc.getElementsByTagName("Operation");
        for (int i = 0; i < list.getLength(); i++) {
            DOMSource src = new DOMSource(list.item(i));
            StringWriter sr = new StringWriter();
            Result res = new StreamResult(sr);
            tx.transform(src, res);
            System.out.println(sr);
        }
    

    输出

    <Operation Name="OperationName1">Entity details1</Operation>
    <Operation Name="OperationName2">Entity details2</Operation>
    <Operation Name="OperationName3">Entity details3</Operation>
    <Operation Name="OperationName4">Entity details4</Operation>
    

    【讨论】:

    • 没有任何默认方法可以给子节点xml吗?因为如果我有非常大的 xml 并且每个子节点都有不同的节点名称和属性,那么这将变得更加复杂。
    • 好的。看看新版本
    【解决方案2】:

    试试这个

    String elemName;
    
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {       
       elemName=qName;
    }
    
    public void characters(char[] ch, int start, int length)
            throws SAXException {  
    if(elemName.equals("OperationName1")) {
     String OperationName1Text=new String(ch);
    }     
    }
    
    public void endElement(String uri, String localName, String qName)
            throws SAXException {       
    }
    

    【讨论】:

    • 抱歉,但这只会返回“Entity Details2”之类的文本,还是我错了?
    • @Giri 是字符方法只返回它。其他方法如开始元素将给出元素名称及其属性。你还想得到什么
    • 谢谢,就像我在帖子中解释的那样,我需要将子节点的整个 xml 存储在字符串变量中。我们可以像 Evgeniy Dorofeev 所说的那样创建它。但我有非常复杂的 xml。所以我正在尝试使用一些默认方法。有可能吗?
    【解决方案3】:

    请看下面的代码,这里我获取了一个子节点“描述”

    URL url;
    
    try {
    
        url = new URL(urls);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        if ((conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc;
            doc = db.parse(url.openStream());
            doc.getDocumentElement().normalize();
    
            NodeList itemLst = doc.getElementsByTagName("item");
            nl = doc.getElementsByTagName(KEY_HEAD);
    
            Description = new String[itemLst.getLength()];// ........
    
    
            for (int i = 0; i < itemLst.getLength(); i++) {
    
                Node item = itemLst.item(i);
                if (item.getNodeType() == Node.ELEMENT_NODE) {
                    Element ielem = (Element) item;
    
                    NodeList description = ielem
                            .getElementsByTagName("description");
    
                    Desc[i] = description.item(0).getChildNodes().item(0)
                            .getNodeValue();
    
                }
    
            }
    
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DOMException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    【讨论】:

    • 对不起,但这也只返回节点内的文本。
    最近更新 更多