无法使用StaXParser解析xml

时间:2019-07-08 10:09:30

标签: java xml stax

我需要使用xml技术来解析StaXParser文件。我尝试过,但是无法生成所需的输出。以下是我的文件:

XML文件

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Author>Google</Author>
  <LastAuthor>Google</LastAuthor>
  <Created>2019-07-08T07:11:02Z</Created>
  <LastSaved>2019-07-08T07:15:23Z</LastSaved>
  <Version>12.00</Version>
 </DocumentProperties>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>4950</WindowHeight>
  <WindowWidth>15120</WindowWidth>
  <WindowTopX>120</WindowTopX>
  <WindowTopY>60</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="9" ss:ExpandedRowCount="3" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Row>
    <Cell><Data ss:Type="String">CategoryName</Data></Cell>
    <Cell><Data ss:Type="String">SongName</Data></Cell>
    <Cell><Data ss:Type="String">AlbumTitle</Data></Cell>
    <Cell><Data ss:Type="String">AlbumDescription</Data></Cell>
    <Cell><Data ss:Type="String">AlbumGenre</Data></Cell>
    <Cell><Data ss:Type="String">AlbumArtist</Data></Cell>
    <Cell><Data ss:Type="String">SongNameAr</Data></Cell>
    <Cell><Data ss:Type="String">AlbumTitleAr</Data></Cell>
    <Cell><Data ss:Type="String">AlbumDescriptionAr</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">Comedy</Data></Cell>
    <Cell><Data ss:Type="String">Aarfek-Nasrawi</Data></Cell>
    <Cell><Data ss:Type="String">Comediah</Data></Cell>
    <Cell><Data ss:Type="String">Comediah</Data></Cell>
    <Cell><Data ss:Type="String">Comediah</Data></Cell>
    <Cell><Data ss:Type="String">nadir-allwlby</Data></Cell>
    <Cell><Data ss:Type="String">عارفك </Data></Cell>
    <Cell><Data ss:Type="String">عارفك </Data></Cell>
    <Cell><Data ss:Type="String">عارفك </Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">Comedy</Data></Cell>
    <Cell><Data ss:Type="String">Ahleen-Ya-Itihadi</Data></Cell>
    <Cell><Data ss:Type="String">Comediah</Data></Cell>
    <Cell><Data ss:Type="String">Comediah</Data></Cell>
    <Cell><Data ss:Type="String">Comediah</Data></Cell>
    <Cell><Data ss:Type="String">nadir-allwlby</Data></Cell>
    <Cell><Data ss:Type="String">عارفك </Data></Cell>
    <Cell><Data ss:Type="String">عارفك </Data></Cell>
    <Cell><Data ss:Type="String">عارفك </Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <Selected/>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Sheet2">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Sheet3">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>

解析器类

package xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;


public class StaXParser2 {
    private static boolean ROW,CELL,DATA;


    public void readConfig(String configFile) {

        ROW=CELL=DATA=false;
        try {
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            InputStream in = new FileInputStream(configFile);
            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);


            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();


                if (event.isStartElement()) {
                    StartElement element = (StartElement)event; 

                    if (element.getName().toString().equalsIgnoreCase("Row")) 
                    { 
                        ROW = true; 
                    } 

                    if (element.getName().toString().equalsIgnoreCase("Cell")) 
                    { 
                        CELL = true; 
                    } 

                    if (element.getName().toString().equalsIgnoreCase("Data")) 
                    { 
                        DATA = true; 
                    } 

                }

                if (event.isEndElement()) 
                { 
                    EndElement element = (EndElement) event; 
                    if (element.getName().toString().equalsIgnoreCase("Row")) 
                    { 
                        ROW = false; 
                    } 

                    if (element.getName().toString().equalsIgnoreCase("Cell")) 
                    { 
                        CELL = false; 
                    } 

                    if (element.getName().toString().equalsIgnoreCase("Data")) 
                    { 
                        DATA = false; 
                    } 

                }

                if (event.isCharacters()) 
                { 

                    Characters element = (Characters) event; 


                    if(ROW) {
                        System.out.println(element.getData());
                    }

                    if(CELL) {
                        System.out.println(element.getData());
                    }
                    if(DATA) {
                        System.out.println(element.getData()); 
                    }
                }




            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
    }

}

主班

package xml;

public class TestRead {
    public static void main(String args[]) {
        StaXParser2 read = new StaXParser2();
        read.readConfig("C:\\Users\\Google\\Desktop\\kkkk\\Comediah\\Comediah\\Local\\Book2.xml");
    }
}

无输出。只是空白。我对其进行了调试,发现它没有进入块以将布尔标志变为true。

1 个答案:

答案 0 :(得分:2)

您应该使用element.getName().getLocalPart()而不是element.getName().toString()

getName包含名称空间urn(在这种情况下,getName()会导致类似{urn:schemas-microsoft-com:office:excel}DATA的情况,因此您的equals检查存在缺陷。