I have to read an xml with an dynamic structure bleow the "sdnEntry" Tag.
With a relative xpath I can read the value or attribute of the tag, but I also need to read the name of the tag.
What must I do be able to read the tag. The function getname allows me to read the first tag, but not in combination with the xpath.
I would be greatful for a hint.
The script below gives me the output: "value: IRAN BAM"
I am missing the output "element: lastName"
The xpath is my given input parameter:
$xpath='/ScanResults[1]/CofiResults[1]/Hit[1]/ListEntry[1]/sdnEntry[1]/*[2]/text()';
/**** msgexport.xml ***/
<?xml version="1.0"?>
<ScanResults><CofiResults>
...
<Hit entityId="0">
<ListEntry group_id="11075">
<sdnEntry>
<uid>11075</uid>
<lastName>IRAN BAM</lastName>
<sdnType>Vessel</sdnType>
<programList>
<program>NPWMD</program>
</programList>
<idList>
<id>
<uid>5567</uid>
<idType>Vessel Registration Identification</idType>
<idNumber>IMO 9323833</idNumber>
</id>
</idList>
<vesselInfo>
<vesselType>Bulk Carrier</vesselType>
<vesselFlag>Malta</vesselFlag>
<vesselOwner>IRISL</vesselOwner>
<tonnage>73664</tonnage>
<grossRegisteredTonnage>40166</grossRegisteredTonnage>
</vesselInfo>
</sdnEntry>
</ListEntry>
<Matches>
<Field invalid="false" path="*[2]/text()" text="IRAN BAM">
<Pattern invalid="false">
........
</Pattern>
</Field>
</Matches>
</Hit>
</ScanResults>
</CofiResults>
With a relative xpath I can read the value or attribute of the tag, but I also need to read the name of the tag.
What must I do be able to read the tag. The function getname allows me to read the first tag, but not in combination with the xpath.
I would be greatful for a hint.
The script below gives me the output: "value: IRAN BAM"
I am missing the output "element: lastName"
The xpath is my given input parameter:
$xpath='/ScanResults[1]/CofiResults[1]/Hit[1]/ListEntry[1]/sdnEntry[1]/*[2]/text()';
/**** msgexport.xml ***/
<?xml version="1.0"?>
<ScanResults><CofiResults>
...
<Hit entityId="0">
<ListEntry group_id="11075">
<sdnEntry>
<uid>11075</uid>
<lastName>IRAN BAM</lastName>
<sdnType>Vessel</sdnType>
<programList>
<program>NPWMD</program>
</programList>
<idList>
<id>
<uid>5567</uid>
<idType>Vessel Registration Identification</idType>
<idNumber>IMO 9323833</idNumber>
</id>
</idList>
<vesselInfo>
<vesselType>Bulk Carrier</vesselType>
<vesselFlag>Malta</vesselFlag>
<vesselOwner>IRISL</vesselOwner>
<tonnage>73664</tonnage>
<grossRegisteredTonnage>40166</grossRegisteredTonnage>
</vesselInfo>
</sdnEntry>
</ListEntry>
<Matches>
<Field invalid="false" path="*[2]/text()" text="IRAN BAM">
<Pattern invalid="false">
........
</Pattern>
</Field>
</Matches>
</Hit>
</ScanResults>
</CofiResults>
PHP-Code:
<?php
$filename="msgexport.xml";
$parser=xml_parser_create(); //define object
xml_parser_set_option($parser,XMLOPTION_FOLDING,0);//stop uppercaseing
$xml_str=implode("",file($filename));
$SimpleXmlElement = new SimpleXMLElement($xml_str);
xml_parser_free($parser); //free object
$html_start=
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head><title>xml read lement from relative path</title></head>
<body>
';
$html_end="</body></html>\n";
$xpath='/ScanResults[1]/CofiResults[1]/Hit[1]/ListEntry[1]/sdnEntry[1]/*[2]/text()';
$value_arr=$SimpleXmlElement->xpath($xpath);
//$element_arr=$SimpleXmlElement->xpath($xpath)->getname();
echo $html_start;
echo "<hr>\n";
echo "<br>\n value: ".$value_arr[0];
//echo "<br>\n element: ".$element_arr[0];
echo "<hr>\n";
echo $html_end;
?>
Kommentar