【问题标题】:Can you help me with this mistake to save a xml file in php?你能帮我解决这个错误以在 php 中保存 xml 文件吗?
【发布时间】:2021-03-12 12:30:02
【问题描述】:

这是我在 PHP 中遇到的错误。 Fatal error: Uncaught Error: Call to undefined method WorldDataParser::saveXML() in /opt/lampp/htdocs/WME/save.php:18 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/WME/save.php on line 18

我想保存 XML,但有一个我真的无能为力的错误?

这个错误到底告诉我什么?

我的代码:

WorldDataParser 类:

function saveXML($xml, $array)
    {
        foreach ($array as $id => $value) {
            if (is_numeric($id)) {
                $id = "country";
            };

            if (is_array($value)) {
                $object = $xml->addChild($id);
                $this->saveXML($object, $value);
            } else {
                $id = strtr($id, " ", "_");
                $xml->addChild($id, $value);
            }
        }
        return $xml->asXML("world_data_v1.xml");
    }

保存XML.php:

<!DOCTYPE html>
<html>
<body>

<?php

    require('world_data_parser.php');

    $worldDataParser = new WorldDataParser();

    $array = $worldDataParser->parseCSV("world_data_v1.csv");

    $xml = new SimpleXMLElement("<?xml version='1.0' encoding='utf-8'?><Countries></Countries>");
    $result = $worldDataParser->saveXML($xml, $array);
    $xml->asXML("world_data.xml");

    if($result){
        print ("XML Savestatus: erfogreich! ("); print_r($result); print (")");
    } else {
        print ("XML Savestatus: nicht erfogreich! ("); print_r($result); print (")");
    }

?>

</body>

</html>

【问题讨论】:

  • 尝试使用public function saveXML($xml, $array) 而不是function saveXML($xml, $array)
  • 我在 vscode 中遇到了这个错误:syntax error, unexpected 'public' (T_PUBLIC), expecting end of file
  • 请附上WorldDataParser的完整代码。有点不对劲。
  • 我有一个新错误:Warning: SimpleXMLElement::asXML(world_data_v1.xml): failed to open stream: Permission denied in /opt/lampp/htdocs/WME/world_data_parser.php on line 40 第一个错误已解决。你能帮我解决新的错误吗?
  • @avydesign 我的意思是在WorldDataParser 类中。不在saveXML.php

标签: php html xml csv xampp


【解决方案1】:

这是总代码:

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
</head>

<body>

    <?php

    class WorldDataParser
    {
        public function parseCSV($csvFile)
        {
            if (($handle = fopen($csvFile, "r")) !== FALSE) {
                $head = fgetcsv($handle);
                while (!feof($handle)) {
                    $array[] = array_combine($head, fgetcsv($handle));
                }
                fclose($handle);
            }
            return $array;
        }

        public function saveXML($xml, $array)
        {
            foreach ($array as $id => $value) {
                if (is_numeric($id)) {
                    $id = "country";
                };

                if (is_array($value)) {
                    $object = $xml->addChild($id);
                    $this->saveXML($object, $value);
                } else {
                    $id = strtr($id, " ", "_");
                    $xml->addChild($id, $value);
                }
            }
            return $xml->asXML("world_data_v1.xml");
        }

        public function printXML($xml, $xsl)
        {

            $domDocumentXSL = new DOMDocument();
            $domDocumentXSL->load($xsl);

            $domDocumentXML = new DOMDocument();
            $domDocumentXML->load($xml);

            $xsltProcessor = new XSLTProcessor();
            $xsltProcessor->importStylesheet($domDocumentXSL);

            return $xsltProcessor->transformToXml($domDocumentXML);
        }
    }   

    ?>

</body>

</html>

【讨论】:

  • 如果您想添加更多详细信息而不是回答问题,您应该编辑您的问题
猜你喜欢
  • 2017-12-23
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多