Php Xml Parser

[Solved] Php Xml Parser | Php - Code Explorer | yomemimo.com
Question : php parse xml

Answered by : nima-kazerouni

$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->plot;

Source : https://www.php.net/manual/en/simplexml.examples-basic.php | Last Update : Wed, 13 Jan 21

Question : PHP XML Expat Parser

Answered by : naly-moslih

<?php
// Initialize the XML parser
$parser=xml_parser_create();
// Function to use at the start of an element
function start($parser,$element_name,$element_attrs) { switch($element_name) { case "NOTE": echo "-- Note --<br>"; break; case "TO": echo "To: "; break; case "FROM": echo "From: "; break; case "HEADING": echo "Heading: "; break; case "BODY": echo "Message: "; }
}
// Function to use at the end of an element
function stop($parser,$element_name) { echo "<br>";
}
// Function to use when finding character data
function char($parser,$data) { echo $data;
}
// Specify element handler
xml_set_element_handler($parser,"start","stop");
// Specify data handler
xml_set_character_data_handler($parser,"char");
// Open XML file
$fp=fopen("note.xml","r");
// Read data
while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)));
}
// Free the XML parser
xml_parser_free($parser);
?>

Source : | Last Update : Mon, 30 May 22

Question : php xml parser

Answered by : tiago-frana

$xmlContent = file_get_contents(__DIR__ . '/myFile.xml');
$contentToObject = new \SimpleXMLElement($xmlContent);
$jsonData = json_encode($contentToObject, JSON_UNESCAPED_SLASHES);

Source : | Last Update : Tue, 27 Sep 22

Question : php xml parser

Answered by : tiago-frana

## Usage
use TiagoFranca\Helpers\Xml\XmlHandler;
$xmlContent = file_get_contents(__DIR__ . '/myFile.xml');
$data = (new XmlHandler())->toArray($xmlContent);
// $data = XmlHandler::toArray($xmlContent);
var_dump($data);
### Class here
<?php
namespace TiagoFranca\Helpers\Xml;
use SimpleXMLElement;
use BadMethodCallException;
class XmlHandler
{ public const UNAVAILABLE_FUNCTIONS = [ // 'toArray', ]; public const AVAILABLE_FUNCTIONS = [ 'parse', 'toJson', 'toObject', 'toArray', ]; /** * xmlParse function * * @param string|null $xmlContent * @return SimpleXMLElement|null */ public static function xmlParse(?string $xmlContent): ?SimpleXMLElement { if (!$xmlContent) { return null; } return new SimpleXMLElement($xmlContent); } /** * xmlToJson function * * @param string|SimpleXMLElement|null $xmlContent * @param integer $flags json_encode $flags * @param integer $depth json_encode $depth * @return string|null */ public static function xmlToJson( string|SimpleXMLElement|null $xmlContent, int $flags = 0, int $depth = 512 ): ?string { if (!$xmlContent) { return null; } $xmlContent = is_string($xmlContent) ? static::xmlParse($xmlContent) : $xmlContent; return json_encode($xmlContent, $flags, $depth) ?: null; } /** * function xmlToObject * * @param string|SimpleXMLElement|null $xmlContent * @return object|null */ public static function xmlToObject(string|SimpleXMLElement|null $xmlContent): ?object { $xmlContent = static::xmlToJson($xmlContent ?: null); if (!$xmlContent) { return null; } return \json_decode($xmlContent, \false); } /** * function xmlToArray * * @param string|SimpleXMLElement|null $xmlContent * @return array|null */ public static function xmlToArray(string|SimpleXMLElement|null $xmlContent): ?array { $xmlContent = static::xmlToJson($xmlContent ?: null); if (!$xmlContent) { return null; } return \json_decode($xmlContent, true); } public function __call(string $method, array $arguments): mixed { if ( !in_array($method, static::AVAILABLE_FUNCTIONS) || in_array($method, static::UNAVAILABLE_FUNCTIONS) ) { throw new BadMethodCallException("Invalid method: ${method}"); } $methodToCall = 'xml' . \ucfirst($method); if (in_array($methodToCall, static::UNAVAILABLE_FUNCTIONS)) { throw new BadMethodCallException("Invalid method: ${method}"); } return call_user_func_array( static::class . '::' . $methodToCall, $arguments ); } public static function __callStatic(string $method, array $arguments) { $availableMethods = static::availableFunctions(); if (!in_array($method, $availableMethods)) { throw new BadMethodCallException("Invalid method: ${method}"); } return (new static())->__call($method, $arguments); } /** * function availableFunctions * * @return array|null */ public static function availableFunctions(): ?array { $allMethods = array_values( \array_unique( \array_merge( \get_class_methods(static::class), static::AVAILABLE_FUNCTIONS ) ) ); return \array_filter( $allMethods, fn ($functionName) => !\in_array($functionName, static::UNAVAILABLE_FUNCTIONS) ); }
}

Source : | Last Update : Tue, 27 Sep 22

Answers related to php xml parser

Code Explorer Popular Question For Php