Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php parse xml

$movies = new SimpleXMLElement($xmlstr);

echo $movies->movie[0]->plot;
Comment

php xml parser

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

PHP XML Expat Parser

<?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);
?>
Comment

php xml parser

## Usage

use TiagoFrancaHelpersXmlXmlHandler;

$xmlContent = file_get_contents(__DIR__ . '/myFile.xml');
$data = (new XmlHandler())->toArray($xmlContent);
// $data = XmlHandler::toArray($xmlContent);
var_dump($data);

### Class here
<?php

namespace TiagoFrancaHelpersXml;

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)
        );
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: sms laravel 
Php :: how to get value in to radio button php 
Php :: laravel eloquent multiple join 
Php :: how to install phpmyadmin on windows 10 
Php :: php require once 
Php :: laravel one command for model table and controller 
Php :: Laravel PackageManifest.php: Undefined index: name 
Php :: laravel nginx 
Php :: license_verify 
Php :: php rce command 
Php :: namespace in php 
Php :: IP Authorization php code 
Php :: php string variable 
Php :: laravel migrate error default character 199 boot 
Php :: check if data inserted in database wordpress plugin 
Php :: php after leave page 
Php :: uft8 json php 
Php :: heap sort php 
Php :: laravel migration drop foreign keys 
Php :: wpmu create user 
Php :: wp wc php if not is single product page 
Php :: laravel password test 
Php :: php draw line pixel 
Php :: strpos 
Php :: phpmailer doesnt work on infinityfree 
Php :: view blob phpmyadmin 
Php :: decrypt md5 php 
Php :: Token capabilities in vault 
Php :: Best documentation tools for php 
Php :: @yield laravel 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =