【问题标题】:Parsing the Xml file which dont have any child nodes解析没有任何子节点的 XML 文件
【发布时间】:2012-12-04 08:04:11
【问题描述】:

我是 android 开发新手,我想使用 Dom Parser 解析一个特定的 xml 文件。 xml结构是这样的。

<Images>
   <image link="a.tgcdn.net/images/products/zoom/e554_android_plush_robot.jpg"/>
   <image link="a.tgcdn.net/images/products/zoom/e554_android_plush_robot.jpg"/>
   <image link="http://cdn.androidpolice.com/wp-content/themes/ap1/images/android1.png"/>
   <image link="http://cdn.androidpolice.com/wp-content/themes/ap1/images/android1.png"/>
   <image link="http://cdn.androidpolice.com/wp-content/themes/ap1/images/android1.png"/>
</Images>

我想将这些 Web 链接添加到数组列表中。 建议我解决它..甚至与此相关的任何链接或教程都会有所帮助。谢谢。

这里是完整的代码..

public class MainActivity extends Activity {


    String xmlurl       = "--url of xml---";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> mImageLink = new ArrayList<String>();
        try {
            URL url = new URL(xmlurl);
            XMLParser   parser  =   new XMLParser();
            String      xml     = parser.getXMLfromUrl(url);
            Document    doc     = parser.getDomElement(xml);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc1 = db.parse(new InputSource(url.openStream()));
            doc1.getDocumentElement().normalize();

            NodeList nodeList = doc1.getElementsByTagName("photo");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Element websiteElement = (Element) nodeList.item(i);
                nodeList = websiteElement.getChildNodes();              
                mImageLink.add(websiteElement.getAttribute("link"));

            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        for(int i=0;i<mImageLink.size();i++){
            Log.d("Photo link --- " + i,mImageLink.get(i));
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

【问题讨论】:

标签: android android-xml android-parser


【解决方案1】:

请使用以下代码使用 DOM Parser 解析上述 XML 文件。

public class MainActivity extends Activity {

    ArrayList<String> mImageLink;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            mImageLink = new ArrayList<String>();

            InputStream is = getResources().openRawResource(R.raw.temp);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(is));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("image");

            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);

                Element fstElmnt = (Element) node;

                mImageLink.add(fstElmnt.getAttribute("link"));

            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }
    }
}

更多信息请参见下面的链接。

XML Parsing Using DOM Parser

【讨论】:

  • 它没有显示任何输出.. 只是一个空白屏幕.. 我也在清单中添加了 INTERNET 权限。
  • 我只是按照您在更改上面提到的需要更改后建议的相同教程进行操作,即使在该教程中,如果您可以看到该代码下方的 cmets,其中一位用户也会遇到同样的问题即在您的同一链接上。
  • @NitinBathija 如果您正在寻找解决方案,请发布您的代码。
  • 我已经发布了我的代码..检查一下,xml文件有这样的url..a.tgcdn.net/images/products/zoom/e554_android_plush_robot.jpg
  • @NitinBathija 您的 XML 文件网址在哪里?
【解决方案2】:

使用 dom4j 库,它是 SAXReader。

    InputStream is = FileUtils.class.getResourceAsStream("file.xml");

    SAXReader reader = new SAXReader();
    org.dom4j.Document doc = reader.read(is);
    is.close();
    Element content = doc.getRootElement();  //this will return the root element in your xml file
    List<Element> methodEls = content.elements("element"); // this will retun List of all Elements with name "element" 

对于 DOM 解析,您可以使用相同的库 DOMReader

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    相关资源
    最近更新 更多