简体   繁体   中英

not able parse xml using StaXParser

I need to parse xml file using StaXParser technology. I tried, but I was not able to generate desired output. Below are my files:

XML File

<?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>

Parser class

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();
        }
    }

}

main class

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");
    }
}

no output. just blank. I debug it, I found it did not entered in block to turn boolean flags to true.

You should use element.getName().getLocalPart() instead of element.getName().toString()

getName includes the namespace urn (in this case the getName() would result in something like {urn:schemas-microsoft-com:office:excel}DATA and therefore your equals check is flawed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM