How to just get the XML numbers

Asked

Viewed 27 times

2

I want to only get the XML numbers with PHP:

<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
...
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
...
</ExcelWorkbook>
<Styles>
...
</Styles>
<ss:Worksheet ss:Name="Planilha1">
<Table ss:StyleID="ta1">
<Column ss:Span="23" ss:Width="48.19"/>
<Column ss:Index="25" ss:Width="74.61"/>
<Column ss:Width="103.95"/>
<Column ss:Span="37" ss:Width="48.19"/>
<Row ss:Height="15">
<Cell>
<Data ss:Type="String">n1</Data>
</Cell>
<Cell>
<Data ss:Type="String">n2</Data>
</Cell>
<Cell>
<Data ss:Type="String">n3</Data>
</Cell>
<Cell>
<Data ss:Type="String">n4</Data>
</Cell>

</Row>
<Row ss:Height="15">
<Cell>
<Data ss:Type="Number">1</Data>
</Cell>
<Cell>
<Data ss:Type="Number">2</Data>
</Cell>
<Cell>
<Data ss:Type="Number">4</Data>
</Cell>
</Row>

<Row ss:Height="15">
<Cell>
<Data ss:Type="Number">2</Data>
</Cell>
<Cell>
<Data ss:Type="Number">6</Data>
</Cell>
<Cell>
<Data ss:Type="Number">8</Data>
</Cell>
<Cell>
<Data ss:Type="Number">9</Data>
</Cell>

Things I’ve tried before:

    $temporario = $_FILES["Arquivo"]["tmp_name"];

    $xml = simplexml_load_file($temporario);


    foreach($xml->Table->Row->Cell as $Cell) {
        print_r("NUMEROS $Cell\n");
    }
    
    foreach($xml->ss:Worksheet->Row->Cell as $Cell) {
        print_r("NUMEROS $Cell\n");
    }
  • 1

    I left an answer to better understand how Simplexmlelement works. I ended up assembling an own XML, because in the question, the XML is incomplete

1 answer

1

First detail: the function simplexml_load_file generates an instance called SimpleXmlElement. And I’m telling you... this class is pretty boring to move.

You use the separator Object (the ->) only to access the nodes.

Example:

<root>
<a>
   <b>
     <c>Eu sou o C</c>
   </b>
</a>
</root>
var_dump($xml->a->b->c);

This will generate:

object(SimpleXMLElement)#2355 (1) {
  [0]=>
  string(1) "Eu sou o C"
}

To access the attributes, you must access how you do with the indexes of the array, using ['attribute name']

Example:

$str = '<root>
<a>
   <b nome="valor" numero="13.55">
     <c>Eu sou o C</c>
   </b>
</a>
</root>';

$xml = simplexml_load_string($str);

var_dump($xml->a->b['numero']);

The result will be:

  [0]=>
  string(5) "13.55"
}

Note: Everything that is returned comes as SimpleXmlElement. So to convert the value, you need to cast as needed:

var_dump((float) $xml->a->b['numero']); // float(13.55)

To access namespaced values, follow the same rule:

echo (float) $xml->a->b['ss:numero'];

Displaying name and attributes using foreach

To better illustrate, I created the following XML.

<root>
    <table>
        <row>
            <cell label="ID">1</cell>
            <cell label="NOME">Wallace</cell>
            <cell label="Número">33.55</cell>
        </row>
        <row>
            <cell label="ID">2</cell>
            <cell label="NOME">Wayne</cell>
            <cell label="Número">21</cell>
        </row>
    </table>
</root>

How did I notice that you are confused also as to the use of foreach, I created an example of how to access child nodes through it.

Behold:

$xml = simplexml_load_file('./dados.xml');


foreach ($xml->table->row as $row) {

    foreach ($row->cell as $cell) {
        echo $cell['label'], ':', $cell, "\n";
    }
}

The result is

ID:1
NOME:Wallace
Número:33.55
ID:2
NOME:Wayne
Número:20.21

Browser other questions tagged

You are not signed in. Login or sign up in order to post.