2013-11-24 2 views
1

구문 분석 할 자산 폴더에 xml 파일이 있습니다.자산 파일에서 XML 구문 분석

<quran> 
    <sura index="1" name="الفاتحة"> 
     <aya index="1" text="In the name of Allah, Most Gracious, Most Merciful."/> 
     <aya index="2" text="Praise be to Allah, the Cherisher and Sustainer of the worlds;"/> 
     <aya index="3" text="Most Gracious, Most Merciful;"/> 
     <aya index="4" text="Master of the Day of Judgment."/> 
     <aya index="5" text="Thee do we worship, and Thine aid we seek."/> 
     <aya index="6" text="Show us the straight way,"/> 
     <aya index="7" text="The way of those on whom Thou hast bestowed Thy Grace, those whose (portion) is not wrath, and who go not astray."/> 
    </sura> 
    <sura index="2" name="البقرة"> 
     <aya index="1" text="A. L. M."/> 
     <aya index="2" text="This is the Book; in it is guidance sure, without doubt, to those who fear Allah;"/> 
     <aya index="3" text="Who believe in the Unseen, are steadfast in prayer, and spend out of what We have provided for them;"/> 
     <aya index="4" text="And who believe in the Revelation sent to thee, and sent before thy time, and (in their hearts) have the assurance of the Hereafter."/> 
     <aya index="5" text="They are on (true) guidance, from their Lord, and it is these who will prosper."/> 
     <aya index="6" text="As to those who reject Faith, it is the same to them whether thou warn them or do not warn them; they will not believe."/> 
     <aya index="7" text="Allah hath set a seal on their hearts and on their hearing, and on their eyes is a veil; great is the penalty they (incur)."/> 
     <aya index="8" text="Of the people there are some who say: &quot;We believe in Allah and the Last Day;&quot; but they do not (really) believe."/> 
     <aya index="9" text="Fain would they deceive Allah and those who believe, but they only deceive themselves, and realise (it) not!"/> 
    </sura> 
</quran> 

누군가가 나를 도와 그리고 난 각 아야 요소에 대한 각 수라 요소 및 인덱스와 텍스트의 인덱스와 이름을 얻기 위해이 XML을 구문 분석 할 수있는 방법을 말해 줄 수 : 다음은은 XML의 구조입니다.

XmlPullParserFactory pullParserFactory; 
     try { 
      pullParserFactory = XmlPullParserFactory.newInstance(); 
      XmlPullParser parser = pullParserFactory.newPullParser(); 

       InputStream in_s = getApplicationContext().getAssets().open("filename.xml"); 
       parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
       parser.setInput(in_s, null); 
     } catch (XmlPullParserException e) { 

      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

감사

답변

2
XPath xpath = XPathFactory.newInstance().newXPath(); 
String expression = "//sura"; 
InputSource inputSource = new InputSource("filename.xml"); 
NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); 

for(int i=0; i<nodes.getLength(); i++) { 
    Node sura = nodes.item(i); 
    NamedNodeMap attributes = sura.getAttributes(); 
    attributes.getNamedItem("name").getNodeValue() 
} 
+0

이 오류가 발생합니다. java.net.MalformedURLException : 프로토콜을 찾을 수 없습니다 : filename.xml. 어쩌면 파일이 assets 폴더에 있기 때문일 것입니다 –

+0

이것은 답이지만 경로는 반드시 new InputSource ("filename.xml")가 아니어야합니다; '대신에 - 새로운 InputSource (getAssets(). open ("filename.xml")); ' –

+0

이 작업의 일부 설명과 함께 좋았을 것입니다. –

1

InputSource의 생성자가 상대 경로를 예상 : 지금은

내가에만이 코드가 있습니다. 따라서 assets 폴더에 있다면 "/assets/filename.xml"을 넣어야합니다.