Home > Backend Development > PHP Problem > How to convert php string to xml

How to convert php string to xml

藏色散人
Release: 2023-03-06 19:28:01
Original
2395 people have browsed it

How to convert php string to xml: First create a PHP sample file; then use the "function array2xml($arr){...}" method to convert php array to xml string.

How to convert php string to xml

Recommended: "PHP Video Tutorial"

php Convert xml string into array and convert array into xml;

1. Convert xml string into php array.

function xmlToArray($xml){
    if (file_exists($xml)) {
        libxml_disable_entity_loader(false);
        $xml_string = simplexml_load_file($xml,'SimpleXMLElement', LIBXML_NOCDATA);
    }else{
        libxml_disable_entity_loader(true);
        $xml_string = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA);
    }
    $result = json_decode(json_encode($xml_string),true);
    return $result;
}
Copy after login

2. Convert php array to xml string

function array2xml($arr){
    $str="<xml>";
    foreach ($arr as $key=>$value){
        $str.= "<$key>$value</$key>";
    }
    $str.="</xml>";
    return $str;
}
Copy after login

The above is the detailed content of How to convert php string to xml. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template