【问题标题】:Why when I send XML to PHP are the nodes lowercase, but when I parse them in PHP they are uppercase?为什么当我将 XML 发送到 PHP 时节​​点是小写的,但是当我在 PHP 中解析它们时它们是大写的?
【发布时间】:2010-10-31 07:11:52
【问题描述】:

这是此答案的后续问题:

How do I parse XML from PHP that was sent to the server as text/xml?

JavaScript/jQuery:

var xmlDoc = jQuery.createXMLDocument( "<items></items>" );
jQuery( "items", xmlDoc ).append( jQuery( "<item>My item!</item>" ) );
jQuery.ajax({
    url: "test.php",
    type: "POST",
    processData: false,
    contentType: "text/xml",
    data: xmlDoc,
    success: function( data ) {
        alert( data );
    }
});

PHP:

$xml_text = file_get_contents("php://input");
$xml = simplexml_load_string($xml_text);
echo $xml->ITEM;

为了让它工作,我必须使用 $xml->ITEM。如果我使用 $xml->item 它将不起作用。我想知道为什么当添加项目节点时它是小写的,但是当我检索它时,它现在必须是大写的?我希望它在我的 PHP 中是小写的。谢谢。

【问题讨论】:

标签: php javascript jquery xml


【解决方案1】:

您的 PHP 脚本接收 My item! 作为输入。试试

<?php
$xml_text = file_get_contents("php://input");
file_put_contents('test.log.txt', $xml_text);
如果您认为不是这样。

xmlDoc 是一个 xml 文档,但 jQuery( "My item!" ) 的结果不是。 一路走来,jQuery 使用 .nodeName,其行为类似于 .tagName 元素。
https://developer.mozilla.org/en/DOM/element.tagName 说:

在 XML(和基于 XML 的语言,如 XHTML)中,tagName 保留大小写。在 HTML 中,tagName 以规范的大写形式返回元素名称。 tagName 的值与 nodeName 的值相同。

【讨论】:

  • 所以我想问题仍然存在,如何让我的 PHP 对 ITEM 节点本身进行不区分大小写的搜索?我一直在寻找 XPath 解决方案或 SimpleXML 解决方案,但找不到。
  • 您不需要进行不区分大小写的搜索。它总是大写的。如果您正在搜索任意节点名称,只需使用 strtoupper() 将其转换为大写即可。
猜你喜欢
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 2015-01-25
  • 1970-01-01
  • 2017-10-01
相关资源
最近更新 更多