【问题标题】:PHP Create well formatted XML out of SQL result [duplicate]PHP从SQL结果中创建格式良好的XML [重复]
【发布时间】:2020-05-12 09:07:47
【问题描述】:

我有问题。我现在正在努力创建一个格式良好的 XML。 我已经创建了这段代码,但我不知道这是否是我需要的:

$sql = "SELECT * FROM Contacts ORDER BY Id ASC";
$result = $conn->query($sql);

$arr_contacts = array();

while ($row = mysqli_fetch_assoc($result))
{
    $contact_array = array(
        $row["Id"]=>array(
        'id'=>$row["Id"],
        'name'=>$row["Name"])
    );

    $arr_contacts = array_merge($arr_contacts, $contact_array);
}

现在我想要一个这样的 XML:

<?xml version="1.0"?>
<Contacts>
    <Contact>
        <id>1</id>
        <name>Smith</name>
    </Contact>
    <Contact>
        <id>2</id>
        <name>John</name>
    </Contact>
    <Contact>
        <id>3</id>
        <name>Viktor</name>
    </Contact>
</Contacts>

问题是我对XML不是很了解,所以我的结果如下:

1Smith2John3Viktor

我用了他的代码:https://www.codexworld.com/convert-array-to-xml-in-php/

我怎样才能得到我想要的结果?

【问题讨论】:

  • 好吧,您应该尝试一下,如果您遇到问题,请发布代码
  • 你在 'name' 中使用了 'naam' 有一个错误
  • 我的错。翻译了,但是忘记了!
  • 我在创建用于 XML 的数组时遇到问题。是数组中的数组中的数组,但是我不知道如何命名。
  • 您的数据库供应商是什么?可以通过 SQL 生成 XML。

标签: php xml


【解决方案1】:

您可以使用以下示例来满足您的需求:

$contact_array = array(
        array(
        'id'=>1,
        'name'=>'Ali'),
        array(
        'id'=>2,
        'name'=>'John'),
        array(
        'id'=>3,
        'name'=>'Victor')
    );

// Function that converts your array to Xml Object
function toXml(SimpleXMLElement $xml, array $data, $mainKey = null)
{   
    foreach ($data as $key => $value) {
        // if the key is an integer, it needs text with it to actually work.
        if (is_numeric($key)) {
            $key = $mainKey ? : "key_$key";
        }
        if (is_array($value)) {
            $childXml = $xml->addChild($key);
            toXml($childXml, $value);
        } else {
            $xml->addChild($key, $value);
        }   
    }   
    return $xml;
}

// Pretty print Xml
function formatXml($simpleXMLElement)
{
    $xmlDocument = new DOMDocument('1.0');
    $xmlDocument->preserveWhiteSpace = false;
    $xmlDocument->formatOutput = true;
    $xmlDocument->loadXML($simpleXMLElement->asXML());

    return $xmlDocument->saveXML();
}

$xml = toXml(new SimpleXMLElement('<Contacts/>'), $contact_array, 'Contact');
// $output = $xml->asXML();
print formatXml($xml);


// Which prints the following
   <?xml version="1.0"?>
    <Contacts>
      <Contact>
        <id>1</id>
        <name>Ali</name>
      </Contact>
      <Contact>
        <id>2</id>
        <name>Vincent</name>
      </Contact>
      <Contact>
        <id>3</id>
        <name>Victor</name>
      </Contact>
    </Contacts>

您可以在这里进行现场测试:http://sandbox.onlinephpfunctions.com/code/230507b1113504fc37427384e3609d0d0cb95f43

注意,您可以忽略元素的键(因此它们仍然是数字),因此它变成并使用与上面示例相同的功能:

$sql = "SELECT * FROM Contacts ORDER BY Id ASC";
$result = $conn->query($sql);

$arr_contacts = array();

while ($row = mysqli_fetch_assoc($result))
{
    $contact_array = array(
        array(
        'id'=>$row["Id"],
        'name'=>$row["Name"])
    );

    $arr_contacts = array_merge($arr_contacts, $contact_array);
}

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2013-11-18
    • 2017-05-26
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多