sun*_*ung 6 php xml arrays parsing loops
我想编写一个函数,将(理论上)未知的XML数据结构解析为等效的PHP数组.
这是我的示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<content>
<title>Sample Text</title>
<introduction>
<paragraph>This is some rudimentary text</paragraph>
</introduction>
<description>
<paragraph>Here is some more text</paragraph>
<paragraph>Even MORE text</paragraph>
<sub_section>
<sub_para>This is a smaller, sub paragraph</sub_para>
<sub_para>This is another smaller, sub paragraph</sub_para>
</sub_section>
</description>
</content>
Run Code Online (Sandbox Code Playgroud)
我从devarticles修改了这个DOM迭代函数:
$data = 'path/to/xmldoc.xml';
$xmlDoc = new DOMDocument(); #create a DOM element
$xmlDoc->load( $data ); #load data into the element
$xmlRoot = $xmlDoc->firstChild; #establish root
function xml2array($node)
{
if ($node->hasChildNodes())
{
$subNodes = $node->childNodes;
foreach ($subNodes as $subNode)
{
#filter node types
if (($subNode->nodeType != 3) || (($subNode->nodeType == 3)))
{
$arraydata[$subNode->nodeName]=$subNode->nodeValue;
}
xml2array($subNode);
}
}
return $arraydata;
}
//The getNodesInfo function call
$xmlarray = xml2array($xmlRoot);
// print the output - with a little bit of formatting for ease of use...
foreach($xmlarray as $xkey)
{
echo"$xkey<br/><br/>";
}
Run Code Online (Sandbox Code Playgroud)
现在,由于我将元素传递给数组的方式,我正在覆盖共享节点名称的任何元素(因为我理想地希望为这些键提供与其原始节点相同的名称).我的递归并不好......但是,即使我清空括号 - 第二层节点仍然作为第一层的值出现(参见描述节点的文本).
任何人都有任何想法,我怎么能更好地构建这个?
| 归档时间: |
|
| 查看次数: |
3277 次 |
| 最近记录: |