setAttribute不保存回到XML页面 [英] setAttribute not saving back to XML page

查看:64
本文介绍了setAttribute不保存回到XML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从XML页面和XML页面提取数据,并尝试使用一些PHP代码将其写回。我已经成功完成了此任务,方法是使用item(0)和 getAttributeNode 拉单个节点,然后保存回 nodeValue setAttribute 。但是,我的目标是使代码与 foreach 语句一起使用,这样,即使添加和删除了路径,也不必回头对其进行编辑。 / p>

如果我尝试使用 setAttribute 进行更改,则在两种情况下均无效(将它们单独或与 foreach 语句),我不知道如何制作 getAttributeNode nodeValue foreach 语句一起使用。我认为我可能需要一个数组,但到目前为止我没有尝试过。我可以加载页面而不会出现错误,但是单击提交按钮后什么也没发生。

 < script src = http://code.jquery.com/jquery-latest.min.js>< / script> 
<?php
$ xml =新的DOMDocument('1.0','utf-8');
$ xml-> formatOutput = true;
$ xml-> preserveWhiteSpace = false;
$ xml-> load('example.xml');

$ xpath =新的DOMXpath($ xml);

$ childForm =’;
foreach($ xpath-> query( // trails / trail)as $ trail){
$ childForm。=< form action =''>
< span class = \ title\>。$ trail-> getAttribute( name)。< / span>< span class = \ title\> Status< / span> ;
< select name = \ asstatusform\>
< option selected value =。$ trail-> getAttribute( status)。>。$ trail -> getAttribute(状态)。< / option>
< option value = \ OPEN\> OPEN< / option>
< option value = \ CLOSED\> CLOSED< / option>
< option value = \ RACING CLOSURE\> RACING CLOSURE< / option>
< / select>< / span> ;
< br>
< / form>;
}

?>

< form action = method = POST>
<?php echo $ childForm; ?>
<输入名称=提交 type = submit value =保存 />
< / form>

<?php
if(isset($ _ POST ['submit']))
{
$ trail-> setAttribute('status',$ _POST ['asstatusform']);
htmlentities($ xml-> save(’example.xml’));
}

?>


解决方案

您说您想使用 foreach语句,但是您在循环之外调用了 setAttribute ,因此您只更改了最后一项。您还会在彼此之间嵌套表单,这是行不通的。



如果我正确理解,您想根据以下情况分别更新每个路径的状态在表单提交上。为此,您需要将数组提交回表单,然后循环遍历。可能棘手的部分是将数组与XML的内容进行匹配。理想情况下,您具有某种唯一标识符,但是在这段代码中,我只是假设XML元素的顺序在显示和处理之间不会改变。因此,第一个POST条目是第一个XML元素,等等。

  <?php 
$ xml = new DOMDocument;
$ xml-> load('example.xml');
$ xpath =新的DOMXpath($ xml);
$ trails = $ xpath-> query( // trails / trail);

if(isset($ _ POST [ submit]))){
foreach($ _POST [ asstatusform] as $ k => $ state){
$ Trails-> item($ k)-> setAttribute( status,$ state);
}
$ xml-> save(’example.xml’);
}
?>

< script src = http://code.jquery.com/jquery-latest.min.js< / script>
< form action = method = POST>
<?php foreach($ trails as $ trail):?>
< span class = title><?= htmlspecialchars($ trail-> getAttribute( name))?>< / span>
< span class = title>状态< / span>
< select name = asstatusform []>
< option selected value =<?= htmlspecialchars($ trail-> getAttribute( status))?>>
<?= htmlspecialchars($ trail-> getAttribute( status))?>
< / option>
< option value = OPEN> OPEN< / option>
< option value = CLOSED> CLOSED< / option>
< option value = RACING CLOSURE> RACING CLOSURE< / option>
< / select>
< / span>
< br />
<?php endforeach; ?>
<输入名称=提交 type = submit value =保存 />
< / form>

您的内联HTML令我头疼,我可以想象您在键入内容时也感到如此。只需打破PHP即可获得一长段HTML。我对<$ c使用了替代语法 $ c> foreach 循环,然后短回显标记用于输出。不要忘记总是使用 htmlspecialchars() 来将输出转义为HTML页面。


I'm pulling data from and XML page and trying to write back to it with some PHP code. I have successfully done this by pulling the individual nodes with item(0) and getAttributeNode, then saving back to nodeValue instead of with setAttribute. My goal, however, is to make the code work with a foreach statement so that I don't have to go back in and edit it if trails are added and removed.

If I try to change it with setAttribute it doesn't work in either scenario (pulling them individually or with the foreach statement), and I can't figure out how to make the getAttributeNode and nodeValue work with a foreach statement. I think I may need an array but nothing I've tried has worked so far. I can get the page to load without an error but nothing happens when I click the submit button.

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<?php
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true; 
$xml->preserveWhiteSpace = false;
$xml->load('example.xml');

$xpath = new DOMXpath($xml);

$childForm = '';
foreach ( $xpath->query("//trails/trail") as $trail )   {
  $childForm .= "<form action=''>
  <span class=\"title\">".$trail->getAttribute("name")."</span> <span class=\"title\">Status</span>
  <select name=\"asstatusform\" >
  <option selected value=".$trail->getAttribute("status").">".$trail->getAttribute("status")."</option>
  <option value=\"OPEN\">OPEN</option>
  <option value=\"CLOSED\">CLOSED</option>
  <option value=\"RACING CLOSURE\">RACING CLOSURE</option>
  </select></span>
  <br>
  </form>";
}

?>

<form action="" method="POST">
    <?php echo $childForm; ?>
    <input name="submit" type="submit" value="Save" />
</form>

<?php
if (isset($_POST['submit']))
{
  $trail->setAttribute('status', $_POST['asstatusform']);
  htmlentities($xml->save('example.xml'));
}

?>

解决方案

You say you want to edit this attribute "with a foreach statement" but you've got your setAttribute call outside the loop so you're only changing the last item. You're also nesting forms inside each other, which does not work.

If I'm understanding correctly, you want to individually update each of the trails' states, based on the form submission. To do that you'll need to submit an array back to the form and then loop through it. The potentially tricky part is matching the array with the the contents of the XML. Ideally you have some kind of unique identifier, but in this code I'm just assuming the order of the XML elements won't change between display and processing. So first POST entry is the first XML element, etc.

<?php
$xml    = new DOMDocument;
$xml    ->load('example.xml');
$xpath  = new DOMXpath($xml);
$trails = $xpath->query("//trails/trail");

if (isset($_POST["submit"])) {
    foreach ($_POST["asstatusform"] as $k=>$state) {
        $trails->item($k)->setAttribute("status", $state);
    }
    $xml->save('example.xml');
}
?>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<form action="" method="POST">
<?php foreach ($trails as $trail): ?>
    <span class="title"><?= htmlspecialchars($trail->getAttribute("name")) ?></span>
    <span class="title">Status</span>
        <select name="asstatusform[]">
            <option selected value="<?= htmlspecialchars($trail->getAttribute("status")) ?>">
                <?= htmlspecialchars($trail->getAttribute("status")) ?>
            </option>
            <option value="OPEN">OPEN</option>
            <option value="CLOSED">CLOSED</option>
            <option value="RACING CLOSURE">RACING CLOSURE</option>
        </select>
    </span>
    <br/>
<?php endforeach; ?>
    <input name="submit" type="submit" value="Save" />
</form>

Your inline HTML was giving me a headache, I can imagine you felt the same typing it out. Just break out of PHP for long blocks of HTML. I've used the alternative syntax for the foreach loop, and short echo tags for output. Don't forget to always use htmlspecialchars() to escape output to HTML pages.

这篇关于setAttribute不保存回到XML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆