如何在不引入列标题的情况下将表中的值转换为XML


How can I get a value from a table into an XML without bringing in the column title?

我正在使用以下函数将数据库中的PaymentAmount转换为XML。

    function GetPaymentTotal($orderID) {
      Global $cnx;
      $ptotal = null;
      $sql    = "SELECT value FROM orders_total WHERE class='ot_total' AND orders_id = '" . $orderID . "'";
      $result = mysql_query($sql, $cnx) or die('Couldn''t get order ot_shipping class from ' . SHOPPING_CART . ' orders_total table: ' . mysql_error());
      if (mysql_num_rows($result) > 0)
      {
          $ptotal = mysql_fetch_assoc($result);
      }  
      return $ptotal;
    }

使用此输出:

$a_order['PaymentAmount']   = GetPaymentTotal($order->OrderNumber);

它起作用了,但出于某种原因,它在我的价值两侧都添加了"价值"标签。。。这是我不想要的。以下是目前的情况:

<PaymentAmount>
   <value>74.8501</value>
</PaymentAmount>

这是我想要的样子。

<PaymentAmount>74.8501</PaymentAmount>

如有任何帮助,我们将不胜感激。

根据您提供的代码,您不喜欢的XML是:

<PaymentAmount>
   <value>74.8501</value>
</PaymentAmount>

是从数据库中提取的。因此,您的问题表明,您已将不需要的数据存储到数据库中。然而,您还没有在问题中展示如何将数据存储在数据库中,因此无法给出如何修复存储数据的具体编程示例。

因此,只有一般的答案似乎适用于说,你需要改变你在数据库中存储数据的方式。

另一方面,当您从数据库中获取数据时,您可以尝试修复数据。然而,这似乎真的很麻烦,因为您需要首先解析数据库中的数据,然后以您想要的格式创建一个新的XML文档:

$dbData
    = '<PaymentAmount>
   <value>74.8501</value>
</PaymentAmount>';
$dbDoc  = new SimpleXMLElement($dbData);             # XML doc from database
$tag    = $dbDoc->getName();                         # Get tag name
$newDoc = new SimpleXMLElement("<$tag/>");           # XML doc for application
$newDoc->{0} = (string) $dbDoc->value[0];            # import value
$newData = explode("'n", $newDoc->asXML(), 2)[1];    # remove XML declaration
echo $newData; # <PaymentAmount>74.8501</PaymentAmount>