php解析xml问题

时间:2013-05-24 21:24:18

标签: php simplexml

我正在尝试解析由excel生成的xml,但我正在寻找一个雄鹿。 我无法到达Workbook节点下面的任何位置:

$benregxml = simplexml_load_file('00_ben_reg_main.xml');
$wrkbook = $benregxml->Workbook->Worksheet;
print_r($wrkbook);

如果我记录工作簿,那就有效(输出是SimpleXMLElement Object())。

我实际上是在尝试获取行节点,但我尝试的任何内容似乎都无法正常工作。

Excel 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>Ognen Plavevski</Author>
        <LastAuthor>Ognen Plavevski</LastAuthor>
        <Created>2013-05-24T20:28:09Z</Created>
        <Version>14.00</Version>
    </DocumentProperties>
    <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
        <AllowPNG/>
    </OfficeDocumentSettings>
    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
        <WindowHeight>10035</WindowHeight>
        <WindowWidth>22995</WindowWidth>
        <WindowTopX>480</WindowTopX>
        <WindowTopY>45</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>
        <Style ss:ID="s62">
            <Protection ss:Protected="0"/>
        </Style>
    </Styles>
    <Worksheet ss:Name="Sheet1">
        <Table ss:ExpandedColumnCount="19" ss:ExpandedRowCount="101" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">
            <Row>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">ID</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">PARENT_RECORD_ID</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">PARENT_PAGE_ID</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">PARENT_ELEMENT_ID</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">CREATED_DATE</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">CREATED_BY</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">CREATED_LOCATION</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">CREATED_DEVICE_ID</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">MODIFIED_DATE</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">MODIFIED_BY</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">MODIFIED_LOCATION</Data>
                </Cell>
                <Cell ss:StyleID="s62">
                    <Data ss:Type="String">MODIFIED_DEVICE_ID</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/>
            <Panes>
                <Pane>
                    <Number>3</Number>
                    <RangeSelection>R1C1:R101C19</RangeSelection>
                </Pane>
            </Panes>
            <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>

1 个答案:

答案 0 :(得分:2)

试试这个

$benregxml = simplexml_load_file('00_ben_reg_main.xml');
$wrkbook = $benregxml->Worksheet[0]->Table->Row;
print_r($wrkbook);

这是你的等级问题,当然你有3个工作表标签(所以它是一个数组)。工作簿标签是头部,所以 从一开始$benregxml = Workbook

<强>更新

$benregxml = simplexml_load_file('00_ben_reg_main.xml');
$cells = $benregxml->Worksheet[0]->Table->Row[0];
//You need Row[0] because Row will return object, but you want just the content, 
    //which is the array of cells.
echo "<pre>"; print_r($cells); echo "</pre>";

echo "<br>----------------------------------------------------------------<br>";
foreach( $cells as $cell ) {

    echo "<pre>"; print_r($cell); echo "</pre>";
}

只是为了澄清,Row返回SimpleXMLElement Object,正如您在输出中看到的那样,由SimpleXMLElement Object个单元格组成。如果你想要你想要的对象Row,但是如果你想要实际的数组你需要Row [0]。如果您发现这很难理解,我建议您检查json格式。它会有很大帮助。希望这会有所帮助。