で複雑なXMLを解析私はSAXパーサーを使用して、このXMLを解析していた一aaplicaitonを持って
http://feed.dinmat.no/android_proxy.php?action=find&find=&path=Svin&amount=15&page=1
私は
与えてください特定のreceipeあちこちこのXML内のすべてのワインのタグを解析したいです私のいくつかのアイデア
package dinmat.android.parser;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.util.Log;
import dinmat.android.R;
import dinmat.android.global.Global;
import dinmat.android.objects.Recipes_Wines;
public class XmlParser extends DefaultHandler
{
public String RootElement;
public String RecordElement;
public String xmlURL;
public Object mainObj;
public Object newObj;
public boolean inProcess;
public ArrayList<Object> Records = null;
private final String TAG = "XmlParser";
StringBuffer buffer = new StringBuffer();
String elementName;
String elementValue;
String im;
Context context;
String NodeIDGEt;
public XmlParser(String strURL,Object tempObj)
{
// xmlURL = strURL;
Log.i("In SAXParser activity", " in XmlParser: *** ");
mainObj = tempObj;
inProcess = false;
xmlURL = strURL;
}
/*public XmlParser(InputStream open, Recipes tempObj)
{
xmlURL = open.toString();
mainObj = tempObj;
inProcess = false;
}*/
public ArrayList<Object> ParseFile(String rootElement ,String recordElement)
{
Log.i("In SAXParser activity", " in ParseFile: *** ");
RootElement = rootElement;
RecordElement = recordElement;
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader reader = sp.getXMLReader();
reader.setContentHandler(this);
Log.d(TAG," xmlURL ::: "+im);
reader.parse(new InputSource(getClass().getResourceAsStream(xmlURL)));
// reader.parse(xmlURL);
// reader.parse(new InputSource(getClass().getResourceAsStream(xmlURL)));
// reader.parse(new InputSource(im));
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
public ArrayList<Object> parseUrl(String rootElement ,String recordElement)
{
RootElement = rootElement;
RecordElement = recordElement;
try{
URL sourceUrl = new URL(xmlURL);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader reader = sp.getXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource(sourceUrl.openStream()));
}catch(Exception e){
e.printStackTrace();
return null;
}
return this.Records;
}
@Override
public void startElement(String Uri, String localName,String qName,Attributes attributes) throws SAXException
{
//Log.i("In SAXParser activity", " in startElement: *** "+localName);
elementValue = "";
if(localName.length() > 0)
{
if(localName.equalsIgnoreCase(RootElement))
{
Records = new ArrayList<Object>();
}
else if(localName.equalsIgnoreCase(RecordElement))
{
newObj = ClassUtils.newObject(mainObj);
ClassUtils.objectMapping(newObj, localName, elementValue);
inProcess = true;
}
else if(localName.equalsIgnoreCase("wines"))
{
Log.e("In SAXParser activity", " in startElement wines: *** ");
XmlParser xmlObjBars = new XmlParser("http://feed.dinmat.no/android_proxy.php?action=find&find=&path=Svin&amount=15&page=1", new Recipes_Wines());//getAssets().open("recipes.xml").toString(), new Recipes());
if(xmlObjBars != null)
{
Global.ayyRecipesWines = xmlObjBars.parseUrl("wines", "wine");
}
String LocationattsValue = attributes.getValue("id");
//Log.e("In SAXParser activity", " startElement wineselementValue :: *** "+ LocationattsValue);
// ArrayList Global.AttributeValues.add(LocationattsValue);
}
else if(localName.equalsIgnoreCase("wine"))
{
String LocationattsValue = attributes.getValue("id");
Global.nodeID.add(NodeIDGEt);
Global.WineId.add(LocationattsValue);
}
}
}
@Override
public void characters(char[] ch,int start,int length) throws SAXException{
elementValue+= new String(ch,start,length).trim();
System.out.println("Element value is "+elementValue);
}
@Override
public void endElement(String Uri,String localName,String qName) throws SAXException
{
Log.i("In SAXParser activity", " in endElement: *** "+localName);
if(localName.equalsIgnoreCase(RecordElement)){
Records.add(newObj);
inProcess = false;
}
else if(localName.equalsIgnoreCase("wines"))
{
Log.e("In SAXParser activity", " in wines: *** "+elementValue+"mainObj.getClass()"+mainObj.getClass());
/*ClassUtils.objectMapping(newObj, localName, elementValue);
Log.e("In SAXParser activity", " in image- url: *** "+elementValue+"mainObj.getClass()"+mainObj.getClass());
System.out.println("ClassNAme"+newObj.getClass().getName());
System.out.println("ClassNAme"+mainObj.getClass().getName());
if(newObj.getClass().getName().equals("com.bourbon.object.Bars"))
{
Global.barsImage.add(elementValue);
System.out.println("Values"+elementValue);
}
if(newObj.getClass().getName().equals("com.bourbon.object.Bourbons"))
{
Global.bourbonsImage.add(elementValue);
System.out.println("Values"+elementValue);
}*/
}
else if(inProcess){
ClassUtils.objectMapping(newObj, localName, elementValue);
}
if(localName.equalsIgnoreCase("node_id"))
{
//System.out.println("Node iD"+elementValue);
NodeIDGEt=elementValue;
}
}
}
ここにSAXハンドラのクラスコードを投稿する –