Display XML parameters in PHP

Asked

Viewed 14 times

0

I’m creating a routine of reading XML files in a directory, but I’m having trouble presenting the XML elements. The XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="http://MedicalRecords" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="MedicalRecords http://bns01/ReportServer?%2FBITQNS%2FMedicalRecords&amp;rs%3ACommand=Render&amp;rs%3AFormat=XML&amp;rs%3ASessionID=fco3lo45kmjbg155n4snltrq&amp;rc%3ASchema=True" Name="MedicalRecords">
   <PacienteSinaisVitais>
      <Report Name="PersonVitalSignsSummary">
         <Tablix4>
            <Details4_Collection>
               <Details4 EventDate4="2019-10-30T18:15:25.393" Peso="82.00" BloodPressureDiastolic2="182.00" BloodPressureStatus2="2.04" IMC="24.80" />
               <Details4 EventDate4="2019-10-17T11:08:57.03" Peso="81.00" BloodPressureDiastolic2="182.00" BloodPressureStatus2="2.02" IMC="24.50" />
            </Details4_Collection>
         </Tablix4>
         <Tablix2 />
         <Tablix3>
            <Details3_Collection>
               <Details3 EventDate3="2019-10-17T11:08:57.03" CardioFreq="76" />
            </Details3_Collection>
         </Tablix3>
         <Tablix5 />
         <Tablix7>
            <Details6_Collection>
               <Details6 EventDate7="2019-10-17T11:08:57.03" CardioFreq4="97" />
            </Details6_Collection>
         </Tablix7>
         <Tablix6 />
      </Report>
   </PacienteSinaisVitais>
   <PacienteEventos>
      <table1_section_Collection>
         <table1_section section="SOAP">
            <Detail_Collection>
               <Detail grupo="Subjetivo" itemDescription="ASSINTOMÁTICO" />
               <Detail grupo="Objetivo" itemDescription="recuperação medular ok&#xA;&#xA;" />
               <Detail grupo="Avaliação" itemDescription="LINFOMA FOLICULAR EC - IV-BS&#xA;1º R-CHOP - 24/10/19" />
               <Detail grupo="Plano" itemDescription="liberado SEGUNDO CICLO DE QT PARA 13/11/19 - 2º  R-CHOP - JÁ AGENDADO NO HNSG&#xA;" />
               <Detail grupo="Observações" />
            </Detail_Collection>
         </table1_section>
         <table1_section section="PEDIDOS PARA RECEPÇÃO">
            <Detail_Collection>
               <Detail grupo="Solicitação" itemDescription="RETORNO - 3 semanas: Consulta marcada para 03/12/2019 10:00" />
               <Detail grupo="Solicitação" itemDescription="LIBERAÇÃO GUIAS: checar agendamento QT 13/11 = hnsg: ok" />
            </Detail_Collection>
         </table1_section>
         <table1_section section="ORDEM DE SERVIÇO">
            <Detail_Collection>
               <Detail grupo="Emissão" itemDescription="Ordem de Serviço para 13/11/2019" />
               <Detail grupo="Emissão" itemDescription="Ordem de Serviço para 14/11/2019" />
               <Detail grupo="Emissão" itemDescription="Ordem de Serviço para 15/11/2019" />
               <Detail grupo="Emissão" itemDescription="Ordem de Serviço para 16/11/2019" />
               <Detail grupo="Emissão" itemDescription="Ordem de Serviço para 17/11/2019" />
            </Detail_Collection>
         </table1_section>
      </table1_section_Collection>
   </PacienteEventos>
</Report>

For example, I want to display the PESO value in index.php:

<?php

$dir = "xml/";
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if (($file !== '.') && ($file !== '..') ) {
        $xml = simplexml_load_file($dir . $file);
        echo $xml->PacienteSinaisVitais->Report->Tablix4->Details4_Collection->Details4->Peso . "<br>";
    }
  }
  closedir($dh);
  }
}

?>

No value is displayed on the page (blank). What is the correct way to reference the variable to display in ECHO?

1 answer

1


uses php like this

<?php
    $xml=simplexml_load_file("arquivo.xml");
    print_r($xml);
?>

or if your xml is string within a variable

<?php
    $xml=simplexml_load_string($xml_em_string) or die("Error: Cannot create object");
    print_r($xml);
?>

This way your xml will be converted into an array and just you work the information you want as independent variable.

to display the weight

echo $xml->PacienteSinaisVitais->Report->Tablix4->Details4_Collection->Details4['Peso'];
  • And how do I present only the variable "weight" for example that is within vital Patients? Since echo $xml->Patientsigns vital->Report->Tablix4->Details4_collection->Details4->Weight did not work

  • Give me a few minutes I’ll spin here and give you the ex working

Browser other questions tagged

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