O
O
Olzhas Ilyubayev2014-03-31 17:24:05
PHP
Olzhas Ilyubayev, 2014-03-31 17:24:05

How to access a tag in an XML tree using SimpleXML in PHP?

Hello!
I'm trying to get strings from an XML file and write them into an array using SimpleXML in PHP.
I open XML, parse using the namespace, but I stopped at a seemingly easy task - I can’t access the tag in which the string is stored.
Actually the question is: how do I access the tag, which contains the line "This is the third answer to question number 1" and write it to an array?
Maybe someone can tell me the path to the tag?

document.xml code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">

<w:body>
<w:tbl>
  <w:tblPr>
    <w:tblStyle w:val="a3"/>
    <w:tblW w:w="0" w:type="auto"/>
    <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
  </w:tblPr>
  <w:tblGrid>
    <w:gridCol w:w="1595"/>
    <w:gridCol w:w="1595"/>
    <w:gridCol w:w="1595"/>
    <w:gridCol w:w="1595"/>
    <w:gridCol w:w="1595"/>
    <w:gridCol w:w="1596"/>
  </w:tblGrid>
  <w:tr w:rsidR="00EE7346" w:rsidTr="00EE7346">
    <w:tc>
      <w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr>
      <w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346">
        <w:r>
          <w:t>Вопрос номер 1</w:t>
        </w:r>
      </w:p>
    </w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это ответ на вопрос номер 1</w:t></w:r></w:p>
    </w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это второй ответ на вопрос номер 1</w:t></w:r></w:p>
    </w:tc>
      <w:tc><w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это третий ответ на вопрос номер 1</w:t></w:r></w:p>
    </w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это четвертый ответ на вопрос номер 1	</w:t></w:r></w:p>
    </w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1596" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это пятый ответ на вопрос номер 1</w:t></w:r></w:p>
    </w:tc>
  </w:tr>
  
  <w:tr w:rsidR="00EE7346" w:rsidTr="00EE7346">
    <w:tc>
      <w:tcPr>
        <w:tcW w:w="1595" w:type="dxa"/>
      </w:tcPr>
      <w:p w:rsidR="00EE7346" w:rsidRPr="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346">
        <w:r>
          <w:t>Вопрос номер 1</w:t>
        </w:r>
      </w:p>
    </w:tc>
    <w:tc>
      <w:tcPr>
        <w:tcW w:w="1595" w:type="dxa"/>
      </w:tcPr>
      <w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346">
        <w:r>
          <w:t>Это первый ответ на вопрос номер 2</w:t>
        </w:r>
      </w:p>
    </w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это второй ответ на вопрос номер 2</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p>
    </w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это третий ответ на вопрос номер 2</w:t></w:r></w:p></w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1595" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это четвертый ответ на вопрос номер 2</w:t></w:r></w:p>
    </w:tc>
    <w:tc>
      <w:tcPr><w:tcW w:w="1596" w:type="dxa"/></w:tcPr><w:p w:rsidR="00EE7346" w:rsidRDefault="00EE7346" w:rsidP="00EE7346"><w:r><w:t>Это пятый ответ на вопрос номер 2</w:t></w:r></w:p>
    </w:tc>
  </w:tr>
</w:tbl>

<w:p w:rsidR="000A6CF5" w:rsidRPr="00EE7346" w:rsidRDefault="000A6CF5" w:rsidP="00EE7346"/><w:sectPr w:rsidR="000A6CF5" w:rsidRPr="00EE7346"><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr>
</w:body>
</w:document>

php code
<?php
$doc = simplexml_load_file("document.xml"); // открываем файл
if ($doc === false) {
  echo 'Error while parsing the document';
  exit;
}

$namespaces = $doc->getNamespaces(true); 
$w = $doc->children($namespaces["w"]); //определяем пространство имен
  
var_dump($w); // получаем дамп

echo $w->body->tr[1]->tc[1]->p->r->t; // пытаемся получить строку <w:t>Вопрос номер 1</w:t> не получается
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stepan, 2014-03-31
@ilyubayev

It is difficult to see the dump in this case. Dump one by one until you find what exactly doesn't work.
var_dump($w->body);
If ok, then
var_dump($w->body->tr);
..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question