Php 将XML节点作为参数传递给对象实例化,然后从中调用子节点

Php 将XML节点作为参数传递给对象实例化,然后从中调用子节点,php,xml,oop,Php,Xml,Oop,我想知道在PHP中,传递一个XML节点然后调用一个方法来访问它是否是合法的语法。我试着转换成字符串,但没有成功。 我做错了什么? 最好/最简单的选择是什么 XML 小部件1 东西 东西 小部件2 PHP preserveWhiteSpace=false; $dom->formatOutput=true; $dom=dom\u import\u simplexml($user)->所有者文档; foreach($user->widgets->widget as$widget){ $new_w

我想知道在PHP中,传递一个XML节点然后调用一个方法来访问它是否是合法的语法。我试着转换成字符串,但没有成功。 我做错了什么? 最好/最简单的选择是什么

XML


小部件1
东西
东西
小部件2

PHP

preserveWhiteSpace=false;
$dom->formatOutput=true;
$dom=dom\u import\u simplexml($user)->所有者文档;
foreach($user->widgets->widget as$widget){
$new_widget=new widget($widget);//节点传递的位置
数组推送($widgets,$new\u widget);
}
//比如说
$new_widget[0]->设置_子节点();
$new_widget[0]->获取_子节点();
类小部件{
私人$widget;
私有$stuffArray=array();
公共函数构造($widget){
$this->widget=$widget;
}
公共函数集_子节点(){
foreach($this->widget->stuff->morestuff as$morestuff=>$value){
$this->stuffArray[$morestuff]=$value;
}
}
公共函数get_子节点(){
foreach($this->stuffArray作为$stuff){
echo$stuff;
}
}
}

确实可以将XML对象作为参数传递给对象,并对其调用方法,但代码中存在许多错误,这些错误使其无法工作。特别是,您使用的XML并不是您认为的结构--
stuff
morestuff
节点不是
小部件的子节点,因此您尝试使用它们执行的任何操作都不会起作用。下面是XML的一个更正版本和一些PHP代码,它们实现了我认为您正在尝试执行的上述操作:

$widgets = array();

# you can load your code from a file, obviously--for the purposes of the example,
# I'm loading mine using a function.
$sxe = simplexml_load_string( get_my_xml() );

foreach ($sxe->widgets->widget as $widget) {
    $new_widget = new Widget($widget);  // Where the node gets passed
    array_push($widgets, $new_widget);
}

// For example
foreach ($widgets as $w) {
    $w->set_subnodes();
    $w->get_subnodes();
}

function get_my_xml() {
    return <<<XML
<user>
    <widgets>
        <widget>Widget 1
            <stuff>
                <morestuff>Things</morestuff>
            </stuff>
            <stuff>
                <morestuff>Other Things</morestuff>
            </stuff>
        </widget>
        <widget>Widget 2
            <stuff>
               <morestuff>Widget Two's Things</morestuff>
            </stuff>
            <stuff>
               <morestuff>Widget Two's Other Things</morestuff>
            </stuff>
        </widget>
    </widgets>
</user>
XML;
}
输出:

pushing Things on to array
pushing Other Things on to array
Running get_subnodes: got Things
Running get_subnodes: got Other Things
pushing Widget Two's Things on to array
pushing Widget Two's Other Things on to array
Running get_subnodes: got Widget Two's Things
Running get_subnodes: got Widget Two's Other Things

您缺少一个字符:
$this->widget->stuff morestuff
此外,类中对
$stuffArray
的引用应该是
$this->stuffArray
@SamDufel抱歉,这是打字错误。这段代码代表了我正在编写的更大的代码,所以我输入了这个示例来更简单地解释这个想法。
$widgets = array();

# you can load your code from a file, obviously--for the purposes of the example,
# I'm loading mine using a function.
$sxe = simplexml_load_string( get_my_xml() );

foreach ($sxe->widgets->widget as $widget) {
    $new_widget = new Widget($widget);  // Where the node gets passed
    array_push($widgets, $new_widget);
}

// For example
foreach ($widgets as $w) {
    $w->set_subnodes();
    $w->get_subnodes();
}

function get_my_xml() {
    return <<<XML
<user>
    <widgets>
        <widget>Widget 1
            <stuff>
                <morestuff>Things</morestuff>
            </stuff>
            <stuff>
                <morestuff>Other Things</morestuff>
            </stuff>
        </widget>
        <widget>Widget 2
            <stuff>
               <morestuff>Widget Two's Things</morestuff>
            </stuff>
            <stuff>
               <morestuff>Widget Two's Other Things</morestuff>
            </stuff>
        </widget>
    </widgets>
</user>
XML;
}
class Widget {

    private $widget;
    private $stuffArray = array();

    public function __construct($widget) {
        $this->widget = $widget;
    }

    public function set_subnodes() {
        # put all the "morestuff" nodes into the stuffArray
        foreach ($this->widget->xpath("stuff/morestuff") as $ms) {
            print "pushing $ms on to array" . PHP_EOL;
            array_push($this->stuffArray, $ms);
        }
    }

    public function get_subnodes() {
        foreach ($this->stuffArray as $stuff) {
            print "Running get_subnodes: got $stuff" . PHP_EOL;
        }
    }
}
pushing Things on to array
pushing Other Things on to array
Running get_subnodes: got Things
Running get_subnodes: got Other Things
pushing Widget Two's Things on to array
pushing Widget Two's Other Things on to array
Running get_subnodes: got Widget Two's Things
Running get_subnodes: got Widget Two's Other Things