使用 fwrite 循环 XML

发布于 2024-12-01 12:19:21 字数 2885 浏览 2 评论 0原文

function xmlParse() {

    $fh = fopen('schools/' . $this->id . '/books/school_books.xml', 'a');


    $xmlstr = "<?xml version='1.0' ?>\n" .
            "<rows></rows>";

    // create the SimpleXMLElement object with an empty <book> element
    $xml = new SimpleXMLElement($xmlstr);


    $x = 0;

    // add some more child nodes
    for ($i = 0; $i < sizeof($this->students); $i++) {

        $this->students[$i]->getmyBooks();


        for ($j = 0; $j < sizeof($this->students[$i]->myBooks); $j++) {



            $row = $xml->addChild("row");
            $row->addAttribute("id", $x);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->owner);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->title);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->category);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->price);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->description);
            $row->addChild("cell", "Test");
            $x++;

            fwrite($fh, $xml->asXML());

        }



    }


}

我知道问题是什么:它的 fwrite($fh, $xml->asXML());

如果我继续在循环中调用它,它不会附加它,它会继续从头开始 xml 文档并再次发布标签。

我的问题是它不断地从头开始写入 xml 标签...而不是仅仅继续写入 xml。如果我只为 1 名学生执行此操作,效果会很完美,但当我尝试循环遍历所有学生时,它会继续打印 xml 标签,而不是继续打印下一个学生。

<?xml version="1.0"?>
   <rows>
     <row id="0">
       <cell>Owner</cell>
       <cell>test</cell>
       <cell>Math</cell>
       <cell>11</cell>           
       <cell>test</cell>
       <cell>Test</cell>
     </row>
   </rows>

   <?xml version="1.0"?>

继续下一个,然后一次又一次地执行 xml 标记。

对于一名学生来说,它是这样的:

<rows>
     <row id="0">
       <cell>Owner</cell>
       <cell>Calculus III</cell>
       <cell>Math</cell>
       <cell>82</cell>
       <cell>This book is in great condition! Available asap.</cell>
       <cell>Test</cell>
     </row>
     <row id="1">
       <cell>Owner</cell>
       <cell>Discrete Mathematics</cell>
       <cell>Math</cell>
       <cell>62</cell>
       <cell>This book is in poor condition.</cell>
       <cell>Test</cell>
     </row>
     <row id="2">
       <cell>Owner</cell>
       <cell>Calculus I</cell>
       <cell>Math</cell>
       <cell>12</cell>
       <cell>Really good book.</cell>
       <cell>Test</cell>
     </row>
    </rows>
function xmlParse() {

    $fh = fopen('schools/' . $this->id . '/books/school_books.xml', 'a');


    $xmlstr = "<?xml version='1.0' ?>\n" .
            "<rows></rows>";

    // create the SimpleXMLElement object with an empty <book> element
    $xml = new SimpleXMLElement($xmlstr);


    $x = 0;

    // add some more child nodes
    for ($i = 0; $i < sizeof($this->students); $i++) {

        $this->students[$i]->getmyBooks();


        for ($j = 0; $j < sizeof($this->students[$i]->myBooks); $j++) {



            $row = $xml->addChild("row");
            $row->addAttribute("id", $x);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->owner);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->title);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->category);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->price);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->description);
            $row->addChild("cell", "Test");
            $x++;

            fwrite($fh, $xml->asXML());

        }



    }


}

I know what the problem is: its fwrite($fh, $xml->asXML());

if I keep calling that within the loop it doesn't append it keeps starting the xml document from scratch and posting the tags again.

My Problem is that it keeps writing from all over again the xml tags... instead of just continuing with the xml. If i do it for just 1 student it works perfect, but instead when I try to loop through all my students it keeps printing the xml tags instead of continuing on to the next student.

<?xml version="1.0"?>
   <rows>
     <row id="0">
       <cell>Owner</cell>
       <cell>test</cell>
       <cell>Math</cell>
       <cell>11</cell>           
       <cell>test</cell>
       <cell>Test</cell>
     </row>
   </rows>

   <?xml version="1.0"?>

continues with the next one then it does xml tags again and again.

This is how its to look like for one student:

<rows>
     <row id="0">
       <cell>Owner</cell>
       <cell>Calculus III</cell>
       <cell>Math</cell>
       <cell>82</cell>
       <cell>This book is in great condition! Available asap.</cell>
       <cell>Test</cell>
     </row>
     <row id="1">
       <cell>Owner</cell>
       <cell>Discrete Mathematics</cell>
       <cell>Math</cell>
       <cell>62</cell>
       <cell>This book is in poor condition.</cell>
       <cell>Test</cell>
     </row>
     <row id="2">
       <cell>Owner</cell>
       <cell>Calculus I</cell>
       <cell>Math</cell>
       <cell>12</cell>
       <cell>Really good book.</cell>
       <cell>Test</cell>
     </row>
    </rows>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2024-12-08 12:19:21

您确实在寻找 file_put_contents($name, $contents)。该函数将所有内容一起添加,因此您可以在循环结束时调用它一次。

代码可能如下所示:

// after i >= sizeof($this->students)
file_put_contents('schools/' . $this->id . '/books/school_books.xml', 
                  $xml->asXML());

另一方面,fwrite 每次调用时都会附加一个文件。这意味着它会将 XML 的内容添加到文件 sizeof($this->students) 次,这就是您现在所看到的。

顺便说一句,根据 sizeof($this->students) 的大小,您可能需要声明一个局部变量来缓存,在您查看之前,sizeof 每次都会被调用。

$studentSize = sizeof($this->students);
for ($i = 0; $i < $studentSize; $i++) {

另一方面,您可能想将其更改为 foreach 循环(目前无法描述如何更改,但如果我记得的话,我会在稍后添加)。

You're really looking for file_put_contents($name, $contents). That function adds all of the content en masse, so you would call it once at the end of the loop.

The code might look like:

// after i >= sizeof($this->students)
file_put_contents('schools/' . $this->id . '/books/school_books.xml', 
                  $xml->asXML());

fwrite on the other hand, appends a file every single time it is called. This means that it will add the contents of the XML to the file sizeof($this->students) times, which is what you're seeing right now.

By the way, depending on the size of sizeof($this->students), you may want to declare a local variable to cache that before you look, sizeof it will be called every time.

$studentSize = sizeof($this->students);
for ($i = 0; $i < $studentSize; $i++) {

On the other hand, you might want to change that to a foreach loop (can't describe how at the moment but I'll add that in later if I remember).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文