2017-10-04 2 views
-3

xml을 텍스트로 파싱하여 블로거를 안드로이드 앱으로 변환 할 계획입니다. 나는 자바 코드로 해왔다. 자, 나는 그것을 활동 목록에 표시하고 싶다. 나는이 자바 코드에 대한 목록보기 XML 코드를 원한다. 이걸로 나를 도울 수 있니?블로거 용 안드로이드의 목록보기

당신은 여기에서 XML 소스를 찾을 수 있습니다 http://vestro-blogger-theme.blogspot.com/feeds/posts/default

public class MainActivity extends AppCompatActivity { 

     String title[]; 
     String content[]; 
     String contentHTML[]; 
     String thumbimage[]; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      // You have to change your blogspot address here 
      // for example : http://YOURBLOGADDRESS.blogspot.com/feeds/posts/default 
      new StartParsing().execute("http://vestro-blogger-theme.blogspot.com/feeds/posts/default"); 
     } 

     private class StartParsing extends AsyncTask<String, Void, Void> { 

      @Override 
      protected Void doInBackground(String... params) { 
       String getURL = params[0]; 

       try { 
        URL url = new URL(getURL); 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
        DocumentBuilder db = dbf.newDocumentBuilder(); 
        Document doc = db.parse(new InputSource(url.openStream())); 
        doc.getDocumentElement().normalize(); 

        NodeList nodeList = doc.getElementsByTagName("title"); 

        title = new String[nodeList.getLength()-1]; 
        content = new String[nodeList.getLength()-1]; 
        contentHTML = new String[nodeList.getLength()-1]; 
        thumbimage = new String[nodeList.getLength()-1]; 

        for (int i = 0; i < nodeList.getLength()-1; i++) { 
         Node node = nodeList.item(i); 

         title[i] = new String(); 
         content[i] = new String(); 
         contentHTML[i] = new String(); 
         thumbimage[i] = new String(); 

         //Get blogspot title 
         NodeList titleList = doc.getElementsByTagName("title"); 
         Element titleElement = (Element)titleList.item(i+1); 
         titleList = titleElement.getChildNodes(); 
         title[i] = ((Node)titleList.item(0)).getNodeValue(); 

         //Get blog spot content as HTML 
         NodeList contentHTMLList = doc.getElementsByTagName("content"); 
         Element contentHTMLElement = (Element)contentHTMLList.item(i+1); 
         contentHTMLList = contentHTMLElement.getChildNodes(); 
         contentHTML[i] = ((Node)contentHTMLList.item(0)).getNodeValue(); 

         // Make plain text from HTML 
         content[i] = String.valueOf(Html.fromHtml(Html.fromHtml(((Node)contentHTMLList.item(0)).getNodeValue()).toString())); 

         // Get Post content thumbnail 
         int start = contentHTML[i].indexOf("src=\"") + 5; 
         int end = contentHTML[i].indexOf("\"",start); 

         thumbimage[i] = contentHTML[i].substring(start,end); 
        } 
       }catch(Exception e) { 
        Log.d("Log","XML parsing exception = "+e); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void aVoid) { 
       super.onPostExecute(aVoid); 

       // Check the data using log 
       Log.d("Log","Title is"); 
       for(int i=0; i<title.length; i++) { 
        Log.d("Log",title[i]); 
       } 
       Log.d("Log","content is"); 
       for(int i=0; i<content.length; i++) { 
        Log.d("Log",content[i]); 
       } 
       Log.d("Log","thumbimage is"); 
       for(int i=0; i<thumbimage.length; i++) { 
        Log.d("Log",thumbimage[i]); 
       } 
       Log.d("Log","contentHTML is"); 
       for(int i=0; i<contentHTML.length; i++) { 
        Log.d("Log",contentHTML[i]); 
       } 
      } 
     } 
    } 

답변

0

나는 앱으로 블로그를 변환하는 XML 데이터를 파싱하는 것을 심각하게 놀랐다. 어쩌면 당신은 그것을 얻을 수 있지만 왜 그런 과정을 처음부터합니까?

BLOGGER API을 사용하여 블로그 콘텐츠를 간단히 구문 분석 할 수 있습니다.

Blogger API 버전 3을 사용하면 클라이언트 응용 프로그램에서 Blogger 콘텐츠를보고 업데이트 할 수 있습니다. 클라이언트 애플리케이션에서 Blogger API v3을 사용하여 새 블로그 게시물을 만들고, 기존 게시물을 수정 또는 삭제하고 특정 기준과 일치하는 게시물 을 쿼리 할 수 ​​있습니다.

사람들이 어디에서나 자신의 게시물을 만들고 관리 할 수있는 브라우저 기반 응용 프로그램 및 모바일 응용 프로그램을 만듭니다.

XML 구문 분석에 도움이 될 수 있지만 간단한 API 호출로 결과를 가져올 수있는 이유는 사용자에게 적합 할 수 있지만 복잡한 작업 일 수 있습니다. 다른 모든 사람들에게 XML 구문 분석 대신 Blogger API를 사용하도록 강력히 권합니다.