如何使用DOM解析器解析XML [英] How to parse the XML using DOM parser
本文介绍了如何使用DOM解析器解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
了如何使用解析器以下XML DOM解析器
how to parser the following XML using DOM PARSER
<Result>
<Status>OK</Status>
<All_BookDetails>
<BookAuthor>Mohammadi Reyshahri</BookAuthor>
<BookRating>0</BookRating>
<BookDescription>Islamic belief and ideology</BookDescription>
<DatePublished>May 1 1992 12:00AM</DatePublished>
<BookTitle>Are You Free or Slave</BookTitle>
<BookID>171</BookID>
<BookCode>EN171</BookCode>
<BookImage>1.jpg</BookImage>
<TotalPages>164</TotalPages>
</All_BookDetails>
</Result>
我想获得 BookAuthor
, BookRating
, BookDescription $的值C $ C>,
DatePublished
, BookTitle
,的BookID
, 图书code
, BookImage
总页数
i want to get the values of BookAuthor
, BookRating
, BookDescription
,DatePublished
, BookTitle
, BookID
, BookCode
, BookImage
TotalPages
我怎么能做到这一点。我试图解析上面的XML选择 All_BookDetails
的父节点,但节点列表
返回我的0长度
how can i do this. I tried to parse the above XML selecting All_BookDetails
as parent node but nodelist
returning me the 0 in length
感谢
推荐答案
获取XML DOM元素
Getting XML DOM element
public Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
return null;
} catch (SAXException e) {
return null;
} catch (IOException e) {
return null;
}
return doc;
}
那么我想这和它的工作
then I tried this and its worked
Document doc = parser.getDomElement(XMLString);
NodeList nl = doc.getElementsByTagName("All_BookDetails");
progressDialog.setCancelable(true);
Element e = (Element) nl.item(0);
BookRating = (Integer.valueOf(parser.getValue(e,
"BookAuthor")));
BookTitle = parser.getValue(e, "BookTitle");
BookAuthor = parser.getValue(e, "BookAuthor");
BookPublishDate = parser.getValue(e, "DatePublished");
BookDescription = parser.getValue(e, "BookDescription");
bookID = parser.getValue(e, "BookID");
bookCode = parser.getValue(e, "BookID");
bookPageCount = parser.getValue(e, "TotalPages");
这篇关于如何使用DOM解析器解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文