2011-07-20 15 views
1

私が作成したXMLファイルを解析して情報を取り出そうとしています。私はthisチュートリアルをベースにしています。解析がXMLファイルokに接続していて、最初のタグを評価しますが、出力に表示されているように後でクラッシュします。以下に、XMLファイルレイアウト、Java解析コード、およびコンソール出力を添付します。SAXパーサーを使用してUTF-8 XMLファイルを解析する際に問題が発生しました

なぜXMLの次のタグを読み取っていないのでしょうか?その場合、私は何を変更する必要がありますか?


XMLファイル:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> 
<filepath>/mnt/sdcard/Audio_Recorder/anonymous22242.3gp</filepath> 
<filename>anonymous22242.3gp</filename> 
<annotation> 
    <file>anonymous22242.3gp</file> 
    <timestamp>0:06</timestamp> 
    <note>test1</note> 
</annotation> 
<annotation> 
    <file>anonymous22242.3gp</file> 
    <timestamp>0:09</timestamp> 
    <note>lol</note> 
</annotation> 
<annotation> 
    <file>anonymous22242.3gp</file> 
    <timestamp>0:09</timestamp> 
    <note>lolol</note> 
</annotation> 

Javaファイル:

private static String fileDirectory; 
private final static ArrayList<String> allFileNames = new ArrayList<String>(); 
private final static ArrayList<String[]> allAnnotations = new ArrayList<String[]>(); 
private static String[] currentAnnotation = new String[3]; 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    try { 

     SAXParserFactory factory = SAXParserFactory.newInstance(); 
     SAXParser playbackParser = factory.newSAXParser(); 

     DefaultHandler handler = new DefaultHandler() { 

      boolean audioFullPath = false; 
      boolean audioName = false; 
      boolean annotationFile = false; 
      boolean annotationTimestamp = false; 
      boolean annotationNote = false; 

      public void startElement(String uri, String localName, 
        String qName, Attributes attributes) 
        throws SAXException { 

       System.out.println("Start Element :" + qName); 

       if (qName.equalsIgnoreCase("filepath")) { 
        audioFullPath = true; 
       } 

       if (qName.equalsIgnoreCase("filename")) { 
        audioName = true; 
       } 

       if (qName.equalsIgnoreCase("file")) { 
        annotationFile = true; 
       } 

       if (qName.equalsIgnoreCase("timestamp")) { 
        annotationTimestamp = true; 
       } 

       if (qName.equalsIgnoreCase("note")) { 
        annotationNote = true; 
       } 

      } 

      public void endElement(String uri, String localName, 
        String qName) throws SAXException { 

       System.out.println("End Element :" + qName); 

      } 

      public void characters(char ch[], int start, int length) 
        throws SAXException { 

       if (audioFullPath) { 
        String filePath = new String(ch, start, length); 
        System.out.println("Full Path : " + filePath); 
        fileDirectory = filePath; 
        audioFullPath = false; 
       } 

       if (audioName) { 
        String fileName = new String(ch, start, length); 
        System.out.println("File Name : " + fileName); 
        allFileNames.add(fileName); 
        audioName = false; 
       } 

       if (annotationFile) { 
        String fileName = new String(ch, start, length); 
        currentAnnotation[0] = fileName; 
        annotationFile = false; 
       } 

       if (annotationTimestamp) { 
        String timestamp = new String(ch, start, length); 
        currentAnnotation[1] = timestamp; 
        annotationTimestamp = false; 
       } 
       if (annotationNote) { 
        String note = new String(ch, start, length); 
        currentAnnotation[2] = note; 
        annotationNote = false; 
        allAnnotations.add(currentAnnotation); 
       } 

      } 

     }; 

     File file = new File(
       "c:\\Documents and Settings\\anonymous.xml"); 
     InputStream inputStream = new FileInputStream(file); 
     Reader xmlReader = new InputStreamReader(inputStream, "UTF-8"); 

     InputSource xmlSource = new InputSource(xmlReader); 
     xmlSource.setEncoding("UTF-8"); 

     playbackParser.parse(xmlSource, handler); 

     System.out.println(fileDirectory); 
     System.out.println(allFileNames); 
     System.out.println(allAnnotations); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

出力:

Start Element :filepath 
Full Path : /mnt/sdcard/Audio_Recorder/anonymous22242.3gp 
End Element :filepath 
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed. 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) 
    at javax.xml.parsers.SAXParser.parse(Unknown Source) 
    at main.main(main.java:131) 

答えて

5

XMLではありません。残りのすべてを含む単一のルート要素が必要です。

+0

私は気が気になりません。しかし、ありがとう! – dancran

2

あなたのXMLはうまく構成されていません。基本(ルート)要素を持つ必要があります。私は<config>要素を追加しました。

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> 
<config> 
    <filepath>/mnt/sdcard/Audio_Recorder/anonymous22242.3gp</filepath> 
    <filename>anonymous22242.3gp</filename> 
    <annotation> 
     <file>anonymous22242.3gp</file> 
     <timestamp>0:06</timestamp> 
     <note>test1</note> 
    </annotation> 
    <annotation> 
     <file>anonymous22242.3gp</file> 
     <timestamp>0:09</timestamp> 
     <note>lol</note> 
    </annotation> 
    <annotation> 
     <file>anonymous22242.3gp</file> 
     <timestamp>0:09</timestamp> 
     <note>lolol</note> 
    </annotation> 
</config> 
+0

それはそれを固定しました... Whoooooooooooops。 – dancran

関連する問題