Проблема с SAX-разбором в Java - PullRequest
2 голосов
/ 27 июня 2011

Итак, я задал вопрос ранее, чтобы прояснить некоторые основы SAX, и я многому научился из ответов.Из того, что я узнал, я попытался создать программу на Java, которая проходила бы через множество каталогов (необходимая часть более крупного проекта, над которым я работаю), а затем внутри каталогов находила файл с именем "document.xml.rels" и использовала * 1002.* чтобы определить элемент 'Target', посмотрите, является ли он файлом изображения (содержит «image» в связанном с ним имени), а затем свяжите цель с атрибутом Id и выполните system.out.print. Я не получаю никаких ошибок откомпилятор или во время выполнения, поэтому мне интересно, правильно ли я просматриваю структуру каталогов или что-то не так с моими условными выражениями в классе SaxHandler?

Просто некоторые замечания ...

Я начинаю в каталоге:

C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items

Я пытаюсь попасть в файл:

C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne/word/_rels/document.xml.rels

Это мой код Java:

import java.io.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class XMLParser
{
    static File directory = new File("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items");
    static File files[] = directory.listFiles();

    public static void main(String[] args) throws IOException
    {
        //For each of the files in "/Extracted Items"...
        for(File f : files)
        {
            //...if it is a directory then...
            if(f.isDirectory())
            {
                //...create a new array populated with each of the files in the directory
                File directoryTwo = new File(f.getAbsolutePath());
                File filesTwo[] = directoryTwo.listFiles();

                //For each of the files in the new directory "/Proposal#"...
                for(File f2 : filesTwo)
                {
                    //...if it is a directory then...
                    if(f2.isDirectory())
                    {
                        //...create a new array populated with each of the files in the directory
                        File directoryThree = new File(f.getAbsolutePath());
                        File filesThree[] = directoryThree.listFiles();

                        //For each of the files in the new directory "/word"
                        for(File f3: filesThree)
                        {
                            //...if it is a directory then...
                            if(f3.isDirectory())
                            {
                                //...create a new array populated with each of the files in the directory
                                File directoryFour = new File(f.getAbsolutePath());
                                File filesFour[] = directoryFour.listFiles();

                                //For each of the files in the new directory "/_rels"
                                for(File f4: filesFour)
                                {
                                    if(f4.getName() == "document.xml.rels")
                                    {
                                        try 
                                        {
                                            // creates and returns new instance of SAX-implementation:
                                            SAXParserFactory factory = SAXParserFactory.newInstance();

                                            // create SAX-parser...
                                            SAXParser parser = factory.newSAXParser();

                                            // .. define our handler:
                                            SaxHandler handler = new SaxHandler();

                                            // and parse:
                                            parser.parse(f3.getAbsolutePath(), handler);    
                                        } 
                                        catch (Exception ex) 
                                        {
                                            ex.printStackTrace(System.out);
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

     private static final class SaxHandler extends DefaultHandler 
     {
         // invoked when document-parsing is started:
         public void startDocument() throws SAXException 
         {
             System.out.println("Document processing started");
         }

         // notifies about finish of parsing:
         public void endDocument() throws SAXException 
         {
             System.out.println("Document processing finished");
         }

         // we enter to element 'qName':
         public void startElement(String uri, String localName, 
                 String qName, Attributes attrs) throws SAXException 
         {
             if(localName.equals("Relationship"))
             {
                 if(attrs.equals("Target"))
                 {
                     if(attrs.getValue("Target").contains("image"))
                     {
                         String id = attrs.getValue("Id");
                         String target = attrs.getValue("Target");
                         System.out.println("Id: " + id + "& Target: " + target);
                     }
                 }
             }  
             else 
             {
                 throw new IllegalArgumentException("Element '" + 
                         qName + "' is not allowed here");
             }
         }

         // we leave element 'qName' without any actions:
         public void endElement(String uri, String localName, String qName)
         throws SAXException 
         {
                // do nothing;
         }
     }
}

Вот документ XML, с которым я работаю

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
      <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml" /> 
      <Relationship Id="rId13" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /> 
      <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml" /> 
      <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header1.xml" /> 
      <Relationship Id="rId12" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml" /> 
      <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /> 
      <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml" /> 
      <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml" /> 
      <Relationship Id="rId11" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image3.png" /> 
      <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml" /> 
      <Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image2.jpeg" /> 
      <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml" /> 
      <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg" /> 
</Relationships>

Новый код Java

import java.io.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class XMLParser
{   
    public static void main(String[] args) throws IOException
    {
        traverse(new File("C:/Documents and Settings/rajeeva/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items"));
    }

     private static final class SaxHandler extends DefaultHandler 
     {
         // invoked when document-parsing is started:
         public void startDocument() throws SAXException 
         {
             System.out.println("Document processing started");
         }

         // notifies about finish of parsing:
         public void endDocument() throws SAXException 
         {
             System.out.println("Document processing finished");
         }

         // we enter to element 'qName':
         public void startElement(String uri, String localName, 
                 String qName, Attributes attrs) throws SAXException 
         {
             if(localName.equals("Relationship"))
             {
                 if(attrs.equals("Target"))
                 {
                     if(attrs.getValue("Target").contains("image"))
                     {
                         String id = attrs.getValue("Id");
                         String target = attrs.getValue("Target");
                         System.out.println("Id: " + id + "& Target: " + target);
                     }
                 }
             }  
             else 
             {
                 throw new IllegalArgumentException("Element '" + 
                         qName + "' is not allowed here");
             }
         }

         // we leave element 'qName' without any actions:
         public void endElement(String uri, String localName, String qName)
         throws SAXException 
         {
                // do nothing;
         }
     }

     private static void traverse(File directory)
     {
        //Get all files in directory
        File[] files = directory.listFiles();
        for (File file : files)
        {
           if (file.isDirectory())
           {
              //It's a directory so (recursively) traverse it
              traverse(file);
           }
           else if (file.getName().equals("document.xml.rels"))
           {
               try 
                {
                    System.out.println("5");
                    // creates and returns new instance of SAX-implementation:
                    SAXParserFactory factory = SAXParserFactory.newInstance();

                    // create SAX-parser...
                    SAXParser parser = factory.newSAXParser();

                    // .. define our handler:
                    SaxHandler handler = new SaxHandler();

                    // and parse:
                    parser.parse(file.getAbsolutePath(), handler);    
                } 
                catch (Exception ex) 
                {
                    ex.printStackTrace(System.out);
                }
            }
         }
     }
}

Новая ошибка

Document processing started
java.lang.IllegalArgumentException: Element 'Relationships' is not allowed here
    at XMLParser$SaxHandler.startElement(XMLParser.java:48)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.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 javax.xml.parsers.SAXParser.parse(Unknown Source)
    at XMLParser.traverse(XMLParser.java:87)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.main(XMLParser.java:13)
5
Document processing started
java.lang.IllegalArgumentException: Element 'Relationships' is not allowed here
    at XMLParser$SaxHandler.startElement(XMLParser.java:48)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.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 javax.xml.parsers.SAXParser.parse(Unknown Source)
    at XMLParser.traverse(XMLParser.java:87)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.main(XMLParser.java:13)
5
Document processing started
java.lang.IllegalArgumentException: Element 'Relationships' is not allowed here
    at XMLParser$SaxHandler.startElement(XMLParser.java:48)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.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 javax.xml.parsers.SAXParser.parse(Unknown Source)
    at XMLParser.traverse(XMLParser.java:87)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.main(XMLParser.java:13)
5
Document processing started
java.lang.IllegalArgumentException: Element 'Relationships' is not allowed here
    at XMLParser$SaxHandler.startElement(XMLParser.java:48)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.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 javax.xml.parsers.SAXParser.parse(Unknown Source)
    at XMLParser.traverse(XMLParser.java:87)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.traverse(XMLParser.java:70)
    at XMLParser.main(XMLParser.java:13)

Есть идеи?

Ответы [ 2 ]

2 голосов
/ 27 июня 2011
       if(f4.getName() == "document.xml.rels")

Следует использовать

       if(f4.getName().equals("document.xml.rels"))

Редактировать: перечитывая ваш код, я вижу еще одну проблему.

             if(attrs.equals("Target"))

attrs относится к типу Attributes, поэтому это сравнение не может быть верным.

0 голосов
/ 28 июня 2011

В сообщении об ошибке, которое вы показали нам «Элемент отношений здесь не разрешен», говорится, что документ недействителен по отношению к его DTD.Но вы не показали нам DTD.

...