2011-10-25 41 views
0

複雜的XML我有一個aaplicaiton在我使用SAX解析器解析這個XML解析Android中

http://feed.dinmat.no/android_proxy.php?action=find&find=&path=Svin&amount=15&page=1

我想分析所有的葡萄酒標籤在該XML來回特定receipe

請給我的一些想法

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表達式:

  • /食譜/食譜/葡萄酒/葡萄酒
  • //酒

第一表達比第二但酒節點更快必須在此路徑能始終被發現。 第二個表達速度較慢(因爲它評估所有節點),但它會找到所有的酒,他們的路徑。

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

0

在我看來,SAX解析器是不是該任務的解決方案。 SAX解析器適用於非常簡單的XML文件,但對於複雜的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。