从android中的xml中获取属性id值

发布于 2024-12-27 12:27:12 字数 720 浏览 1 评论 0原文

请帮我用给定 xml 中的属性解析 xml..

 <tarifs>
<tarif type="internet" name="ОнЛайм 4" price="300" speedin="4" speedout="2"/>
<tarif type="internet" name="ОнЛайм 10" price="400" speedin="10" speedout="5"/>
<tarif type="internet" name="ОнЛайм 20" price="500" speedin="20" speedout="10"/>
</tarifs>

我有 treid 这个

NodeList node = element.getElementsByTagName("tarif");
for (int j = 0; j < node.getLength(); j++) {                        
    initialValues.put(TRAILER_ID, j);                   
    initialValues.put(TRAILER_TITLE, node.item(j).ATTRIBUTE_NODE.getNamedItem("name").nodeValue);
}

但它对我不起作用..请任何人帮助我解决这个问题

提前致谢

please help me to parse the xml with attributes from the given xml..

 <tarifs>
<tarif type="internet" name="ОнЛайм 4" price="300" speedin="4" speedout="2"/>
<tarif type="internet" name="ОнЛайм 10" price="400" speedin="10" speedout="5"/>
<tarif type="internet" name="ОнЛайм 20" price="500" speedin="20" speedout="10"/>
</tarifs>

I have treid this

NodeList node = element.getElementsByTagName("tarif");
for (int j = 0; j < node.getLength(); j++) {                        
    initialValues.put(TRAILER_ID, j);                   
    initialValues.put(TRAILER_TITLE, node.item(j).ATTRIBUTE_NODE.getNamedItem("name").nodeValue);
}

But its not working for me.. PLease anyone help me to solve this

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尽揽少女心 2025-01-03 12:27:12

尝试这样:

NodeList node = element.getElementsByTagName("tarif");
int length = node.getLength();
for (int j = 0; j < length; j++)
{
     Element terrif = (Element) node.item(j);

     String name = terrif.getAttribute("name");
     initialValues.put(TRAILER_TITLE,name);

     // and so on for other attributes...
}

try this way:

NodeList node = element.getElementsByTagName("tarif");
int length = node.getLength();
for (int j = 0; j < length; j++)
{
     Element terrif = (Element) node.item(j);

     String name = terrif.getAttribute("name");
     initialValues.put(TRAILER_TITLE,name);

     // and so on for other attributes...
}
独享拥抱 2025-01-03 12:27:12

试试这个:

public void vParseXMLList(String sXMLResult)
    {
        org.xml.sax.helpers.DefaultHandler handler = new DefaultHandler() 
        {
            boolean bIsMachineNameFound = false;
            boolean bIsMachineSizeFound = false;
            boolean bIsCreateDateFound = false;
            boolean bIsMachineGUIDFound = false;
            boolean bIsTimelinePasswordFound = false;
            boolean bSHAPassword = false;
            boolean bGUIDPassword = false;

            public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException 
            {
                if (localName.equalsIgnoreCase("tarif")) {
                    if(attributes.getValue("type") != null){
                        String sType = attributes.getValue("type"); 
                    }

                    if(attributes.getValue("name") != null){
                        String sName = attributes.getValue("name"); 
                    }

                    if(attributes.getValue("price") != null){
                        int nPrice = Integer.parseInt(attributes.getValue("price")); 
                    }

                    if(attributes.getValue("speedout") != null){
                        int nSpeedOut = Integer.parseInt(attributes.getValue("speedout")); 
                    }

                    if(attributes.getValue("speedin") != null){
                        int nSpeedIn = Integer.parseInt(attributes.getValue("speedin")); 
                    }
                }
            }

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

            public void characters(char ch[], int start, int length) throws SAXException 
            {
            }
        };
        SAXParserFactory saxParserFactory;
        SAXParser saxParser;
        InputStream inputStream;

        try {
                saxParserFactory = SAXParserFactory.newInstance();
                saxParser = saxParserFactory.newSAXParser();
                inputStream = new ByteArrayInputStream(sXMLResult.getBytes("UTF-8"));
                saxParser.parse(inputStream, handler);

        } 
        catch (UnsupportedEncodingException e) {} 
        catch (ParserConfigurationException e) {} 
        catch (SAXException e) {} 
        catch (IOException e) {}
        finally
        {
            saxParserFactory = null;
            saxParser = null;
            inputStream = null;
        }
    }

Try this :

public void vParseXMLList(String sXMLResult)
    {
        org.xml.sax.helpers.DefaultHandler handler = new DefaultHandler() 
        {
            boolean bIsMachineNameFound = false;
            boolean bIsMachineSizeFound = false;
            boolean bIsCreateDateFound = false;
            boolean bIsMachineGUIDFound = false;
            boolean bIsTimelinePasswordFound = false;
            boolean bSHAPassword = false;
            boolean bGUIDPassword = false;

            public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException 
            {
                if (localName.equalsIgnoreCase("tarif")) {
                    if(attributes.getValue("type") != null){
                        String sType = attributes.getValue("type"); 
                    }

                    if(attributes.getValue("name") != null){
                        String sName = attributes.getValue("name"); 
                    }

                    if(attributes.getValue("price") != null){
                        int nPrice = Integer.parseInt(attributes.getValue("price")); 
                    }

                    if(attributes.getValue("speedout") != null){
                        int nSpeedOut = Integer.parseInt(attributes.getValue("speedout")); 
                    }

                    if(attributes.getValue("speedin") != null){
                        int nSpeedIn = Integer.parseInt(attributes.getValue("speedin")); 
                    }
                }
            }

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

            public void characters(char ch[], int start, int length) throws SAXException 
            {
            }
        };
        SAXParserFactory saxParserFactory;
        SAXParser saxParser;
        InputStream inputStream;

        try {
                saxParserFactory = SAXParserFactory.newInstance();
                saxParser = saxParserFactory.newSAXParser();
                inputStream = new ByteArrayInputStream(sXMLResult.getBytes("UTF-8"));
                saxParser.parse(inputStream, handler);

        } 
        catch (UnsupportedEncodingException e) {} 
        catch (ParserConfigurationException e) {} 
        catch (SAXException e) {} 
        catch (IOException e) {}
        finally
        {
            saxParserFactory = null;
            saxParser = null;
            inputStream = null;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文