[英]Error when trying to parse xml using laravel
我正在嘗試解析以下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'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.