php - 尝试使用 laravel 解析 xml 时出错
问题描述
我正在尝试解析以下 xml 并继续遇到相同的错误消息“无法从字符串解析 XML”,请参阅以下错误消息。
protected function resolveXmlObject($xml): Document
{
if (! $xml) {
throw new InvalidContentException('Unable to parse XML from string.');
}
return $this->document->setContent($xml);
}
我是使用 xml 的新手,我不确定我提供的代码有什么问题。如果有人能指出我做错了什么的正确方向,那将不胜感激。
控制器
use Orchestra\Parser\Xml\Facade as XmlParser;
public function upload()
{
$xml = XmlParser::load(asset('xml/data.xml'));
$user = $xml->parse([
'id' => ['uses' => 'programme::id'],
'name' => ['uses' => 'programme.name'],
]);
return view ('projects.upload', compact('user'));
}
XML
<?xml version="1.0" encoding=”UTF-8”?>
<programme data>
<title>Programme Data</title>
<programme id="1">
<programme 1>
<name>A nightmare on Elm Street</name>
<image path>../images/Elm Street.jpg</image path>
<mood>Scared</mood>
</programme 1>
</programme>
看法
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Moodslider</h1>
<div class="box">
<td style="text-align: center; vertical-align: middle;">
</div>
<div class="slidecontainer">Agitated
<input type="range" min="1" max="5" value="1" class="slider" id="myRange">Calm
</div>
<div class="slidecontainer">Happy
<input type="range" min="1" max="5" value="1" class="slider" id="myRange">Sad
</div>
<div class="slidecontainer">Tired
<input type="range" min="1" max="5" value="1" class="slider" id="myRange">Wide Awake
</div>
<div class="slidecontainer">Scared
<input type="range" min="1" max="5" value="1" class="slider" id="myRange">Fearless
</div>
<form action="/action_page.php">
<input type="text" name="fname" placeholder="No content 1"><br>
<input type="text" name="lname" placeholder="No content 2"><br>
</form>
<form action="/projects/upload" method="get" enctype="multipart/form- data">Select image to upload:
<!--{{ csrf_field() }} -->
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
解决方案
调用asset('xml/data.xml')
不会为您提供文件或内部路径,它会为您提供文件public
夹内文件的公共 URL。试试看public_path('xml/data.xml')
。
https://laravel.com/docs/5.7/helpers#method-public-path
例如
$xml = XmlParser::load(public_path('xml/data.xml'));
推荐阅读
- javascript - 返回相反结果的子字符串
- ubuntu - 在使用 Ubuntu 18.04 的笔记本电脑上使用基于 libpcap 的软件
- javascript - 简化导入 - 性能受到影响?
- flutter - 从不同的有状态小部件更新值
- javascript - asp.net core 3 axios 没有按预期发布
- html - 文本换行在错误的位置
- docker - 跨机器时无法连接覆盖网络中的其他容器
- node.js - 如何在 typeorm 的自定义字段上做 orderBy?
- java - Kafka Producer 将消息发布到单个分区
- html - 如何使按钮:当通过代码将按钮置于光标下时,在移动光标之前未触发悬停状态