问候
有人对递归函数有一些经验吗?
我正在尝试将XML文件解析为对象(在PHP中)(稍后在drupal中使用它) 我写的代码是在第一个“children”节点之后几乎总是相同的(见下文)。 改变的唯一两件事是来自xpath的链接(不应该那么困难)和创建动态对象变量(如果存在)。
第一个主题的代码(整个过程是一个课程)
'$ item-> ident< - 因为你在foreach
而闻名$ resulty = $ xpaths-> xpath(“/ org.olat.course.Structure / rootNode / children / [ident = '$ item-> ident'] / children / [type ='st'或type ='sp'或type = 'BC']“);
foreach ($resulty as $itemy) { $SubjectObject = new subject(); // isnt 100% correct but doesn't matter now $SubjectObject->getSubjectFolders($FolderObject); $SubjectObject->setSubjectId(isset($item->SubjectId) ? (string) $item->SubjectId: null); $SubjectObject->setSubjectShortTitle(isset($item->SubjectName) ? (string) $item->SubjectName: null); $ChapterObject->setSubject($SubjectObject); }
现在 - 如果我想要来自某个科目的科目的科目而不是
$ resulty = $ xpaths-> xpath(“/ org.olat.course.Structure / rootNode / children / [ident ='$ item-> ident'] / children / [ type ='st'或type ='sp'或type ='bc']“);
foreach ($resulty as $itemy) {
$SubjectObject = new subject();
$SubjectObject->getSubjectFolders($FolderObject);
$SubjectObject->setSubjectId(isset($itemy->SubjectId) ? (string) $itemy->SubjectId: null);
$SubjectObject->setSubjectShortTitle(isset($itemy->SubjectName) ? (string) $itemy->SubjectName: null);
$resultz = $xpaths->xpath("/org.olat.course.Structure/rootNode/children/*[ident = '$item->ident']children/*[ident = '$itemy->ident']/children/*[type = 'st' or type = 'sp' or type = 'bc']");
foreach ($resultz as $itemz) {
$SubSubjectObject = new subject();
$SubSubjectObject->getSubjectFolders($FolderObject);
$SubSubjectObject->setSubjectId(isset($itemz->SubjectId) ? (string) $itemz->SubjectId: null);
$SubSubjectObject->setSubjectShortTitle(isset($itemz->SubjectName) ? (string) $itemz->SubjectName: null);
$resulta = $xpaths->xpath("/org.olat.course.Structure/rootNode/children/*[ident = '$item->ident']/children/*[ident = '$itemy->ident']/children/*[ident = '$itemz->ident']/children/*[type = 'st' or type = 'sp' or type = 'bc']");
foreach ($resulta as $itema) {
$SubSubSubSubjectObject= new subject();
$SubSubSubSubjectObject->getSubjectFolders($FolderObject);
$SubSubSubSubjectObject->setSubjectId(isset($itema->SubjectId) ? (string) $itema->SubjectId: null);
$SubSubSubSubjectObject->setSubjectShortTitle(isset($itema->SubjectName) ? (string) $itema->SubjectName: null);
(and so on)
$SubSubSubjectObject->setSubject($SubSubSubSubjectObject);
}
$SubjectObject->setSubject($SubSubjectObject);
}
$ChapterObject->setSubject($SubjectObject);
}
这里是xml树(示例)
<Structure>
<rootnode>
<CursusName>History</CursusName>
<CursusId>100001</CursusId>
<children>
<childNode>
<SubjectName>World War 1</SubjectName>
<SubjectId>100002</SubjectId>
<children>
<childNode>
<SubjectName>World War 1.1</SubjectName>
<SubjectId>100016</SubjectId>
<children>
<childNode>
<SubjectName>World War 1.1.1</SubjectName>
<SubjectId>100032</SubjectId>
<children>
<childNode>
<SubjectName>World War 1.1.1.1</SubjectName>
<SubjectId>100065</SubjectId>
</children>
</childNode>
</children>
+<children></children>
</childNode>
</children>
+<children></children>
</childNode>
+<childNode></childNode>
</children>
+<children></children>
</childNode>
+<childNode></childNode>
+<childNode></childNode>
</children>
+<visibility></visibility>
+<accessibility></accessibility>
</rootnode>
<version>1</version>
</Structure>
SO
有人可以帮我吗? 有人知道如何解决对象的问题吗? 一种foreach / while循环创建对象?并将它们提取到他的父对象中?
抱歉长篇文章和所有这些代码行
亲切的问候
迪特尔
亲切的问候
答案 0 :(得分:0)
function recurseResults($resulty)
{
foreach ($resulty as $itemy)
{
$SubjectObject = new subject();
$SubjectObject->getSubjectFolders($FolderObject);
$SubjectObject->setSubjectId(isset($itemy->SubjectId) ? (string) $itemy->SubjectId: null);
$SubjectObject->setSubjectShortTitle(isset($itemy->SubjectName) ? (string) $itemy->SubjectName: null);
$resultz = $xpaths->xpath("/org.olat.course.Structure/rootNode/children/*[ident = '$item->ident']children/*[ident = '$itemy->ident']/children/*[type = 'st' or type = 'sp' or type = 'bc']");
// __save my new subject somewhere__
recurseResults($resultz);
}
}