问题描述
我试图通过解析的Java XML解析,但我得到后 org.apache.harmony.xml.dom.DocumentImpl@418b4c98
。
下面就是我特林解析XML,例如,我需要 ID
数据 5
和 default_image
是 https://www.10ngah.com/api/images/products/5/5
XML
< prestashop的xmlns:XLink的=http://www.w3.org/1999/xlink>
<产品>
<产品>
<&ID GT;
&所述;![CDATA [5]]≥
< / ID>
< id_default_image的XLink:HREF =https://www.10ngah.com/api/images/products/5/5not_filterable =真>
&所述;![CDATA [5]]≥
< / id_default_image>
<价格>
&所述;![CDATA [525]]≥
< /价格>
<名称>
<语言ID =1的XLink:HREF =https://www.10ngah.com/api/languages/1>
<![CDATA [iPad 2的GB与蜂窝]>
< /语言>
< /名称>
< /产品>
<产品>
<&ID GT;
&所述;![CDATA [6]]≥
< / ID>
< id_default_image的XLink:HREF =https://www.10ngah.com/api/images/products/6/6not_filterable =真>
&所述;![CDATA [6]]≥
< / id_default_image>
<价格>
&所述;![CDATA [525]]≥
< /价格>
<名称>
<语言ID =1的XLink:HREF =https://www.10ngah.com/api/languages/1>
<![CDATA [iPad 2的GB与蜂窝]>
< /语言>
< /名称>
< /产品>
< /产品>
< / prestashop>
解析code
包COM prestoshop.xmlparser。进口的java.util.List;
进口org.w3c.dom.Document中;
进口org.w3c.dom.Element中;
进口org.w3c.dom.Node中;
进口org.w3c.dom.NodeList;
进口android.app.Activity;
进口android.os.AsyncTask;
进口android.util.Log;
进口android.widget.BaseAdapter;
进口android.widget.Toast;
进口玉米prestoshop.beans.ProductItems。
进口玉米prestoshop.utils.Utils。公共类ProductLoaderTask扩展的AsyncTask<无效,ProductItems,太虚> {私人活动mContext;
私人列表< ProductItems>产品;
私人BaseAdapter适配器;公共ProductLoaderTask(Activity上下文,列表< ProductItems> productList的,
BaseAdapter适配器){
mContext =背景;
产品= productList的;
this.adapter =适配器;
}静态最后弦乐KEY_ preSTASHOP =prestashop
静态最后弦乐KEY_ALL_PRODUCTS =产品;@覆盖
保护无效doInBackground(虚空...... PARAMS){ 字符串URL = Utils.PRODUCTS_URL; //使用可变参数,就像一个数组
Log.e(urlll,+ URL);
XMLParser的解析器=新XMLParser的();
字符串XML = parser.getXmlFromUrl(URL);
Log.e(串的xml,+ XML);
文档的DOC = parser.getDomElement(XML,mContext);
节点列表NL = doc.getElementsByTagName(产品);
Log.e(noddd,+ NL);
的for(int i = 0; I< nl.getLength();我++){
元素e =(元)nl.item(I)
Log.e(EEEE,+ e.toString());
Log.e(节点列表,
+ parser.getValue(即价格)的toString()); publishProgress(新ProductItems(
parser.getValue(即名),
parser.getValue(即,ID),parser.getValue(E,
id_default_image)12.050000)); } 返回null;
}@覆盖
保护无效onProgressUpdate(ProductItems ...值){ ProductItems产物=值[0];
products.add(产品);
adapter.notifyDataSetChanged();
}@覆盖
保护无效onPostExecute(虚空结果){
Log.e(所有做,products.toString());
super.onPostExecute(结果);
}
}
使用 XmlPullParser
下面的文档的
复制的XML以资产产生的文件夹在本地解析器(仅用于测试)。你可以从URL中的XML和解析。
InputStream为= MainActivity.this.getResources()
.getAssets()开(xmlparser.xml);
新parserPull(是);
然后解析
公共类parserPull
{ 私有静态最后弦乐NS = NULL;
公共parserPull(打开的InputStream){
尝试
{
XmlPullParser解析器= Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,FALSE);
parser.setInput(开,NULL);
parser.nextTag();
清单<钥匙进入GT;所有= readFeed(分析器);
的for(int i = 0; I< all.size();我++)
{
Log.i(ID是..........,all.get(ⅰ).ID);
Log.i(Link是........,all.get(我).LINK);
Log.i(价格是......,all.get(I)。价格);
}
}赶上(例外五)
{
e.printStackTrace();
}
}
私人列表<钥匙进入GT; readFeed(XmlPullParser解析器)抛出XmlPullParserException,IOException异常{
清单<钥匙进入GT;进入= NULL;
parser.require(XmlPullParser.START_TAG,NS,prestashop);
而(parser.next()!= XmlPullParser.END_TAG){
如果(parser.getEventType()!= XmlPullParser.START_TAG){
继续;
}
字符串名称= parser.getName();
//Log.i(\"..................\",name);
//通过查找prestashop标签开始
如果(name.equals(产品)){
进入= readProducts(分析器);
}其他{
跳过(分析器);
}
}
返回进入;
}
私人列表<钥匙进入GT; readProducts(XmlPullParser解析器)抛出XmlPullParserException,IOException异常{
清单<钥匙进入GT;项=新的ArrayList<钥匙进入GT;(); parser.require(XmlPullParser.START_TAG,NS,产品);
而(parser.next()!= XmlPullParser.END_TAG){
如果(parser.getEventType()!= XmlPullParser.START_TAG){
继续;
}
字符串名称= parser.getName();
// Log.i(..................,名);
//通过寻找的产品标签开始
如果(name.equals(产品)){
entries.add(readEntry(分析器));
}其他{
跳过(分析器);
}
}
返回条目;
}
私人资金进入readEntry(XmlPullParser解析器)抛出XmlPullParserException,IOException异常{
parser.require(XmlPullParser.START_TAG,NS,产品);
字符串title = NULL;
字符串摘要= NULL;
字符串link = NULL;
而(parser.next()!= XmlPullParser.END_TAG){
如果(parser.getEventType()!= XmlPullParser.START_TAG){
继续;
}
字符串名称= parser.getName();
// Log.i(...................,名);
如果(name.equals(ID)){
标题= readId(分析器);
}否则如果(name.equals(id_default_image)){
总结=的readlink(分析器);
}否则如果(name.equals(价格)){
链接= readPrice(分析器);
}其他{
跳过(分析器);
}
}
返回新条目(标题,摘要,链接);
}
私人字符串readPrice(XmlPullParser解析器)抛出IOException异常,XmlPullParserException {
parser.require(XmlPullParser.START_TAG,NS,价格);
字符串摘要= READTEXT(分析器);
parser.require(XmlPullParser.END_TAG,NS,价格);
返回总结;
}
私人字符串的readlink(XmlPullParser解析器)抛出IOException异常,XmlPullParserException {
字符串link =;
parser.require(XmlPullParser.START_TAG,NS,id_default_image);
字符串标记= parser.getName();
// Log.i(............标签);
字符串relType = parser.getAttributeValue(NULL,not_filterable);
如果(tag.equals(id_default_image)){
如果(relType.equals(真)){
链接= parser.getAttributeValue(空的XLink:HREF);
parser.nextTag();
}
}
parser.require(XmlPullParser.END_TAG,NS,id_default_image);
返回的链接;
}
私人字符串readId(XmlPullParser解析器)抛出IOException异常,XmlPullParserException {
parser.require(XmlPullParser.START_TAG,NS,ID);
字符串title = READTEXT(分析器);
parser.require(XmlPullParser.END_TAG,NS,ID);
返回称号;
}
私人字符串READTEXT(XmlPullParser解析器)抛出IOException异常,XmlPullParserException {
字符串结果=;
如果(parser.next()== XmlPullParser.TEXT){
结果= parser.getText();
parser.nextTag();
}
返回结果;
}
私人无效跳过(XmlPullParser解析器)抛出XmlPullParserException,IOException异常{
如果(parser.getEventType()!= XmlPullParser.START_TAG){
抛出新IllegalStateException异常();
}
INT深度= 1;
而(深度!= 0){
开关(parser.next()){
案例XmlPullParser.END_TAG:
深度 - ;
打破;
案例XmlPullParser.START_TAG:
深入++;
打破;
}
}
}
公共静态类条目{
公众最终的字符串ID;
公共最后弦乐链接;
公共最后弦乐的价格; 私人资金进入(字符型,字符串link,字符串价格){
this.id = ID;
this.link =链接;
this.price =价格;
}
}
}
日志输出
3月12日至10日:29:44.664:I / ID为..........(1511):5
三月12日至10日:29:44.664:I / Link是........(1511):https://www.10ngah.com/api/images/products/5/5
三月12日至10日:29:44.674:I /价格是.......(1511):525
三月12日至10日:29:44.674:I / ID为..........(1511):6
三月12日至10日:29:44.674:I / Link是........(1511):https://www.10ngah.com/api/images/products/6/6
三月12日至10日:29:44.674:I /价格是.......(1511):525
I am trying to parse xml through JAVA but after parsing I get org.apache.harmony.xml.dom.DocumentImpl@418b4c98
.
Here is the XML what I am tring to parse, for example, i need id
data is 5
and default_image
is https://www.10ngah.com/api/images/products/5/5
Xml
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product>
<id>
<![CDATA[ 5 ]]>
</id>
<id_default_image xlink:href="https://www.10ngah.com/api/images/products/5/5"not_filterable="true">
<![CDATA[ 5 ]]>
</id_default_image>
<price>
<![CDATA[ 525 ]]>
</price>
<name>
<language id="1" xlink:href="https://www.10ngah.com/api/languages/1">
<![CDATA[ iPad 2 GB with Cellular ]]>
</language>
</name>
</product>
<product>
<id>
<![CDATA[ 6 ]]>
</id>
<id_default_image xlink:href="https://www.10ngah.com/api/images/products/6/6" not_filterable="true">
<![CDATA[ 6 ]]>
</id_default_image>
<price>
<![CDATA[ 525 ]]>
</price>
<name>
<language id="1" xlink:href="https://www.10ngah.com/api/languages/1">
<![CDATA[ iPad 2 GB with Cellular ]]>
</language>
</name>
</product>
</products>
</prestashop>
Parsing code
package com.prestoshop.xmlparser;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.Toast;
import com.prestoshop.beans.ProductItems;
import com.prestoshop.utils.Utils;
public class ProductLoaderTask extends AsyncTask<Void, ProductItems, Void> {
private Activity mContext;
private List<ProductItems> products;
private BaseAdapter adapter;
public ProductLoaderTask(Activity context, List<ProductItems> productList,
BaseAdapter adapter) {
mContext = context;
products = productList;
this.adapter = adapter;
}
static final String KEY_PRESTASHOP = "prestashop";
static final String KEY_ALL_PRODUCTS = "products";
@Override
protected Void doInBackground(Void... params) {
String url = Utils.PRODUCTS_URL;// use varargs just like an array
Log.e("urlll", "" + url);
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url);
Log.e("string xml", "" + xml);
Document doc = parser.getDomElement(xml, mContext);
NodeList nl = doc.getElementsByTagName("product");
Log.e("noddd",""+nl);
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
Log.e("eeee", "" + e.toString());
Log.e("nodelist", ""
+ parser.getValue(e, "price").toString());
publishProgress(new ProductItems(
parser.getValue(e, "name"),
parser.getValue(e, "id"), parser.getValue(e,
"id_default_image"), 12.050000));
}
return null;
}
@Override
protected void onProgressUpdate(ProductItems... values) {
ProductItems product = values[0];
products.add(product);
adapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Void result) {
Log.e("All done", products.toString());
super.onPostExecute(result);
}
}
Using XmlPullParser
following the docs http://developer.android.com/training/basics/network-ops/xml.html
Copied xml to assests folder to parser locally ( for testing only ). You can get the xml from the url and parse.
InputStream is = MainActivity.this.getResources()
.getAssets().open("xmlparser.xml");
new parserPull(is);
Then to parse
public class parserPull
{
private static final String ns = null;
public parserPull(InputStream open) {
try
{
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(open, null);
parser.nextTag();
List<Entry> all = readFeed(parser);
for(int i=0;i<all.size();i++)
{
Log.i("ID is..........",all.get(i).id);
Log.i("Link is........",all.get(i).link);
Log.i("Price is.......",all.get(i).price);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entry = null;
parser.require(XmlPullParser.START_TAG, ns, "prestashop");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
//Log.i("..................",name);
// Starts by looking for the prestashop tag
if (name.equals("products")) {
entry= readProducts(parser);
} else {
skip(parser);
}
}
return entry;
}
private List<Entry> readProducts(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "products");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Log.i("..................",name);
// Starts by looking for the products tag
if (name.equals("product")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "product");
String title = null;
String summary = null;
String link = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Log.i("...................",name);
if (name.equals("id")) {
title = readId(parser);
} else if (name.equals("id_default_image")) {
summary = readLink(parser);
} else if (name.equals("price")) {
link = readPrice(parser);
} else {
skip(parser);
}
}
return new Entry(title, summary, link);
}
private String readPrice(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "price");
String summary = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "price");
return summary;
}
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
String link = "";
parser.require(XmlPullParser.START_TAG, ns, "id_default_image");
String tag = parser.getName();
// Log.i("............",tag);
String relType = parser.getAttributeValue(null, "not_filterable");
if (tag.equals("id_default_image")) {
if (relType.equals("true")){
link = parser.getAttributeValue(null, "xlink:href");
parser.nextTag();
}
}
parser.require(XmlPullParser.END_TAG, ns, "id_default_image");
return link;
}
private String readId(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "id");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "id");
return title;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
public static class Entry {
public final String id;
public final String link;
public final String price;
private Entry(String id, String link, String price) {
this.id = id;
this.link = link;
this.price = price;
}
}
}
The log output
12-10 03:29:44.664: I/ID is..........(1511): 5
12-10 03:29:44.664: I/Link is........(1511): https://www.10ngah.com/api/images/products/5/5
12-10 03:29:44.674: I/Price is.......(1511): 525
12-10 03:29:44.674: I/ID is..........(1511): 6
12-10 03:29:44.674: I/Link is........(1511): https://www.10ngah.com/api/images/products/6/6
12-10 03:29:44.674: I/Price is.......(1511): 525
这篇关于麻烦XML通过Java解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!