我是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>
我想在arraylist中添加这些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;
}
}
答案 0 :(得分:0)
使用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解析,您可以使用相同的lib DOMReader
答案 1 :(得分:0)
请使用以下代码使用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);
}
}
}
请参阅以下链接以获取更多信息。