添加新项目时出错


Error adding new item

一切顺利。使用crxml存储库时,未正确生成xml文档。当您在文档中添加新元素时,就会发生这种情况。行动方针:首先,我创建文档

        $this->genXml->Item['Type'] = 'view';
        $this->genXml->Item->{'http://'.$this->siteUrll.'|Name'}    = 'Last View';
        $this->genXml->Item->LastView->View->Time = $app['Time'];
        $this->genXml->Item->LastView->View->Action = $app['Action'];
        $this->genXml->Item->LastView->View->IP = $app['IP'];
        return $this->genXml->xml();

我得到了这种xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Item Type="view">
  <Name xmlns="http://sitename.com">Last View</Name>
  <LastView>
    <View>
      <Time>11:45:12</Time>
      <Action>Click</Action>
      <IP>192.168.1.1</IP>
    </View>
  </LastView>
</Item>

对最终结果进一步添加新值

            $GetFile = <<<EOB
        <?xml version="1.0" encoding="UTF-8"?>
            <Item Type="view">
              <Name xmlns="http://sitename.com">Last View</Name>
              <LastView>
                <View>
                  <Time>11:45:12</Time>
                  <Action>Click</Action>
                  <IP>192.168.1.1</IP>
                </View>
              </LastView>
            </Item>
        EOB;
            $this->genXml->loadXML($GetFile);
            $this->genXml->Item->LastView->View[2]->Time = $app['Time'];
            $this->genXml->Item->LastView->View[2]->Action = $app['Action'];
            $this->genXml->Item->LastView->View[2]->IP = $app['IP'];
            echo($this->genXml->xml());

并获取错误代码xml

<?xml version="1.0" encoding="UTF-8"?>
<Item Type="view">
      <Name xmlns="http://sitename.com">Last View</Name>
      <LastView>
        <View>
          <Time>11:45:12</Time>
          <Action>Click</Action>
          <IP>192.168.1.1</IP>
        </View>
      <View/><View><Time>11:45:12</Time><Action>Click</Action>   <IP>192.168.1.1</IP></View></LastView>
    </Item>

即标签是的地方

<View/>    

帮助解决输出问题。也许我做错了什么?(对不起我的英语,我知道我没有我想要的那么好)只需提供到存储库的链接和对问题的描述。

它在某种程度上很搞笑,因为它是信息学的一个典型问题。我们喜欢从0开始计数。您创建了ID为0(隐式)的第一个视图。如果您现在添加一个ID为2的新视图,它将错过ID 1,而只是插入一个空视图。因此,结果在语法上是正确的。

您只需要将添加的视图的索引更改为1即可防止这种情况发生。