PHP + XML-如何使用SimpleXML或DOMDocument重命名和删除XML元素? [英] PHP + XML - how to rename and delete XML elements using SimpleXML or DOMDocument?

查看:100
本文介绍了PHP + XML-如何使用SimpleXML或DOMDocument重命名和删除XML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于StackOverflow社区的帮助,我修改了复杂的XML源以用于jsTree,因此取得了一些成功。但是,现在有了可用的数据,只有当我手动编辑XML来执行以下操作时,才这样:

I've had some success due to the help of StackOverflow community to modify a complex XML source for use with jsTree. However now that I have data that is usable, it is only so if i manually edit the XML to do the following :


  1. 重命名所有< user> 标记为< item>

  2. 之前删除一些元素第一个< user> 标记

  3. 插入一个'encoding = UTF-8'到XML开启器中

  4. ,最后将< response> (打开XML标记)修改为< root>

  1. Rename all <user> tags to <item>
  2. Remove some elements before the first <user> tag
  3. insert an 'encoding=UTF-8' into the XML opener
  4. and lastly modify the <response> (opening XML tag) to <root>

XML文件示例: SampleXML

我已经在这里和Google上阅读并阅读了很多页面,但是无法找到实现以上各项的方法。

I have read and read through so many pages on here and google but cannot find a method to achieve the above items.

点(2)我发现是通过 SimpleXML 并使用 UNSET 我可以删除这些部分我不需要,但是我仍然

Point (2) I have found out that by loading it via SimpleXML and using UNSET i can delete the portions I do not require, however I am still having troubles with the rest.

我想我可以用 SimpleXML (我比较熟悉),然后继续通过之前提供的帮助来修改代码。

I thought I could perhaps modify the source with SimpleXML (that I am more familiar with) and then continue to modify the code via the help I had been provided before.

<?php
$s = file_get_contents('http://www.fluffyduck.com.au/sampleXML.xml');
$doc1 = simplexml_load_string($s);

unset($doc1->row);
unset($doc1->display);

#$moo = $doc1->user;
echo '<textarea>';
echo $doc1->asXML();
echo '</textarea>';

$doc = new DOMDocument();
$doc->loadXML($doc1);

$users = $doc->getElementsByTagName("user");
foreach ($users as $user)
{
    if ($user->hasAttributes())
    {
        // create content node
        $content = $user->appendChild($doc->createElement("content"));
        // transform attributes into content elements
        for ($i = 0; $i < $user->attributes->length; $i++)
        {
            $attr = $user->attributes->item($i);
            if (strtolower($attr->name) != "id")
            {
                if ($user->removeAttribute($attr->name))
                {
                        if($attr->name == "username") {
                                $content->appendChild($doc->createElement('name', $attr->value));
                        } else {
                            $content->appendChild($doc->createElement($attr->name, $attr->value));
                        }
                    $i--;
                }
            }
        }
    }
}
$doc->saveXML();

header("Content-Type: text/xml");
echo $doc->saveXML();

?>


推荐答案

使用递归,您可以创建一个基于在输入上,一次求解所有点:

Using recursion, you can create a brand new document based on the input, solving all your points at once:

代码

<?php

$input = file_get_contents('http://www.fluffyduck.com.au/sampleXML.xml');
$inputDoc = new DOMDocument();
$inputDoc->loadXML($input);

$outputDoc = new DOMDocument("1.0", "utf-8");
$outputDoc->appendChild($outputDoc->createElement("root"));

function ConvertUserToItem($outputDoc, $inputNode, $outputNode)
{
    if ($inputNode->hasChildNodes())
    {
        foreach ($inputNode->childNodes as $inputChild)
        {
            if (strtolower($inputChild->nodeName) == "user")
            {
                $outputChild = $outputDoc->createElement("item");
                $outputNode->appendChild($outputChild);
                // read input attributes and convert them to nodes
                if ($inputChild->hasAttributes())
                {
                    $outputContent = $outputDoc->createElement("content");
                    foreach ($inputChild->attributes as $attribute)
                    {
                        if (strtolower($attribute->name) != "id")
                        {
                            $outputContent->appendChild($outputDoc->createElement($attribute->name, $attribute->value));
                        }
                        else
                        {
                            $outputChild->setAttribute($attribute->name, $attribute->value);
                        }
                    }               
                    $outputChild->appendChild($outputContent);
                }
                // recursive call
                ConvertUserToItem($outputDoc, $inputChild, $outputChild);
            }
        }
    }
}

ConvertUserToItem($outputDoc, $inputDoc->documentElement, $outputDoc->documentElement);

header("Content-Type: text/xml; charset=" . $outputDoc->encoding);
echo $outputDoc->saveXML();
?>

输出

<?xml version="1.0" encoding="utf-8"?>
<root>
    <item id="41">
        <content>
            <username>bsmain</username>
            <firstname>Boss</firstname>
            <lastname>MyTest</lastname>
            <fullname>Test Name</fullname>
            <email>lalal@test.com</email>
            <logins>1964</logins>
            <lastseen>11/09/2012</lastseen>
        </content>
        <item id="61">
            <content>
                <username>underling</username>
                <firstname>Under</firstname>
                <lastname>MyTest</lastname>
                <fullname>Test Name</fullname>
                <email>lalal@test.com</email>
                <logins>4</logins>
                <lastseen>08/09/2009</lastseen>
            </content>
        </item>
...

这篇关于PHP + XML-如何使用SimpleXML或DOMDocument重命名和删除XML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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