gpt4 book ai didi

java - 通过JAVA解析xml的问题

转载 作者:行者123 更新时间:2023-12-02 06:26:23 25 4
gpt4 key购买 nike

我试图通过JAVA解析xml,但解析后我得到org.apache.harmony.xml.dom.DocumentImpl@418b4c98

这是我要解析的XML,例如,我需要id数据是5default_image 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>

解析代码

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);
}
}

最佳答案

按照文档 http://developer.android.com/training/basics/network-ops/xml.html 使用 XmlPullParser

将 xml 复制到assests文件夹以本地解析器(仅用于测试)。您可以从 url 获取 xml 并解析。

 InputStream is = MainActivity.this.getResources()
.getAssets().open("xmlparser.xml");
new parserPull(is);

然后进行解析

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;
}
}
}

日志输出

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

关于java - 通过JAVA解析xml的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20488449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com