2011-10-25 7 views
0
アンドロイド

で複雑な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; 

     } 

    } 
} 
+0

ここにSAXハンドラのクラスコードを投稿する –

答えて

0

私はあなただけのすべてのあなたのワインのタグを取得するためにXPathを使用することができると思う

Xpath式:

  • /レシピ/レシピ/ワイン/ワイン
  • //ワイン

最初の式が二けど、ワインのノードがこのパスに常にある必要がありますよりも高速であります見つけられた。 2番目の式は(すべてのノードを評価するため)より遅くなりますが、すべてのワイン無条件パスが見つかります。私の意見で

http://developer.android.com/reference/javax/xml/xpath/package-summary.html

0

、SAXパーサーは、このタスクのためのソリューションではありません。非常に単純なXMLファイルではSAXパーサーは問題ありませんが、複雑なXMLファイルの場合は、コードを読みにくくなります。私はXmlBeansを使用するための助言をします - あなたはXSDファイルが利用可能であると仮定します。

しかし、あなたはここで、いくつかのヒントSAXを使用して設定されているもの:

  1. onStartElement(recipe)inRecipe=true;
  2. onStartElement(node_id)if (inRecipe) { inNodeId=true; }
  3. onEndElement(node_id)if (inRecipe) { inNodeId=false; }
  4. onCharacters(String chars)if (inNodeId && (chars.equals("[desired recipe id]"))) { inDesiredRecipe=true; }
  5. onStartElement(wines)を:if (inDesiredRecipe) { inWines = true; }
  6. onStartElement(wine, String id)if(inWines) { wineList.add(id); }
  7. onEndElement(wines)inWines = false;
  8. onEndElement(recipe)inDesiredRecipe=false; inRecipe=false;

ちょうどあなたにそれを行うことができる方法のアイデアを与えるために - あなたが希望するレシピのためのIDを与えるとのリストを取得しますそのレシピに関連するすべてのワインのID。しかし、あなたはまだあなたのコードをどうにか乱雑にするワインについての情報を全部入手しなければならないので、SAXを使わないのです。

関連する問題