目前,我正在尝试在xml文件中获取所有必需的数据,然后插入到数组中。
我不能在此处放置XML文件的链接,因为其中包含我公司的私人数据,但我将模拟其中一个。查看以下内容:
<main>
<item>
<title>
House 1
</title>
<text>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</text>
<link>
http://server25.dataxex.com/priv/auth/data/1/
</link>
</item>
<item>
<title>
House 2
</title>
<text>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</text>
<link>
http://server25.dataxex.com/priv/auth/data/2/
</link>
</item>
<item>
<title>
House 3
</title>
<text>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</text>
<link>
http://server25.dataxex.com/priv/auth/data/3/
</link>
</item>
...
</main>
我正在尝试创建一个脚本,该脚本将读取所有内容并获取全部,然后在此之后将其插入到数组中。
此刻,我只有这段代码:
header('Content-Type: application/json; charset=utf-8');
$feed = "https://<private link>.com/data.xml";
$page = file_get_contents($feed);
$doc = new DOMDocument();
@$doc->loadHTML($page);
$title = $doc->getElementsByTagName('title');
$text = $doc->getElementsByTagName('text');
$link = $doc->getElementsByTagName('link');
foreach($arr as $doc) {
$arr = array("title"=>$title,"text"=>$text,"link"=>$link);
}
var_dump($arr);
此代码不起作用。你能帮助我吗?谢谢!!
答案 0 :(得分:1)
您需要遍历所有item元素并获取其中的元素内容。
<?php
$doc = new DOMDocument();
$doc->loadXML($xml);
$output = [];
$items = $doc->getElementsByTagName('item');
$fields = ['title','text','link'];
foreach($items as $currItemNode)
{
$currOutput = [];
foreach($fields as $currFieldName)
{
$currValue = $currItemNode->getElementsByTagName($currFieldName)[0]->nodeValue;
$currOutput[$currFieldName] = trim($currValue);
}
$output[] = $currOutput;
}
var_dump($output);
编辑:
对于较旧的PHP版本(5.6.3之前的版本),您必须对getElementsByTagName的结果进行一些不同的处理,您必须这样做:
<?php
$doc = new DOMDocument();
$doc->loadXML($xml);
$output = [];
$items = $doc->getElementsByTagName('item');
$fields = ['title','text','link'];
for ($i = 0; $i < $items->length; $i++)
{
$currOutput = [];
foreach($fields as $currFieldName)
{
$currValue = $items->item($i)->getElementsByTagName($currFieldName)->item(0)->nodeValue;
$currOutput[$currFieldName] = trim($currValue);
}
$output[] = $currOutput;
}
var_dump($output);
答案 1 :(得分:0)
所以您可以签出xml_parse_into_struct
$xmlparser = xml_parser_create();
xml_parse_into_struct($xmlparser,$page,$values);
xml_parser_free($xmlparser);
foreach($values as $value) {
print_r($value);
}
这会将XML文件中的所有内容放入一个数组中。
答案 2 :(得分:0)
$xml = simplexml_load_string($xml_variable)
$json = json_encode($xml);
$array = json_decode($json,TRUE);
// loop through the array
for($array as $arr){
// Use var_dump($arr); to debug your array tree
}
Simple xml作为参考