标签: simplexml

使用SimpleXMLElement时添加命名空间

这就是我所追求的

<!-- language: lang-xml -->
<ws:Test>
    <ws:somename2>somevalue2</ws:somename2>
    <ws:make>
        <ws:model>foo</ws:model>
        <ws:model>bar</ws:model>
    </ws:make>
</ws:Test>
Run Code Online (Sandbox Code Playgroud)

这是我目前的代码

<!-- language: lang-php -->
$xmlTest = new SimpleXMLElement('<Test/>', 0, false, 'ws', true);
$xmlTest->addChild("ws:somename2", "somevalue2", 'http://microsoft.com/wsdl/types/');
$make = $xmlTest->addChild('ws:make', null, 'ws');
#$make->addAttribute('name','Ford');
$make->addChild('ws:model', 'foo', 'ws');
$make->addChild('ws:model', 'bar', 'ws');
header ("Content-Type:text/xml");
print_r($xmlTest->asXML());
Run Code Online (Sandbox Code Playgroud)

但它输出

<!-- language: lang-xml -->
<Test>
    <ws:somename2>somevalue2</ws:somename2>
    <ws:make>
        <ws:model>foo</ws:model>
        <ws:model>bar</ws:model>
    </ws:make>
</Test>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,测试中缺少ws:

php simplexml

15
推荐指数
2
解决办法
1万
查看次数

使用命名空间时无法找到SimpleXMLElement

我正在尝试在Wordpress中创建一个Widget,我遇到了一个创建SimpleXMLElement对象的问题.

这是代码:

namespace GenieKnows_Search;  

class GenieKnows_Search_Widget extends \WP_Widget {
     //Constructor
     function __construct() {
         parent::__construct('genieknows_search_widget', 'GenieKnows_Search_Widget');
     }

     //Irrelevant Code. Removed for readability. 

    //Return the XML
    function retrieve_gk_xml() {
          $xml = new SimpleXMLElement($this->create_gk_xml(), 0, true); //Line 114
          return $xml->xpath('/feed/results/sponsored/listing');
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

PHP致命错误:第114行/var/www/myticketpick.com/wp-content/plugins/genieknows-search/genieknows_search.php中找不到类'GenieKnows_Search\SimpleXMLElement'

它似乎正试图在我的GenieKnows_Search命名空间内寻找SimpleXML类,但是我不知道为什么.

有关此错误发生原因的任何想法,以及我如何解决它?

php wordpress namespaces class simplexml

15
推荐指数
1
解决办法
2万
查看次数

大文件上的simplexml_load_string错误发生在一个系统上,而不是另一个系统上

我正在处理一个我无法编辑的第三方PHP库,它已经运行了近一年.它用于simplexml_load_string远程服务器的响应.最近,它一直在大吵大闹.这是房地产列表的数据馈送,格式如下所示:

<?xml version="1.0"?>
<RETS ReplyCode="0" ReplyText="Operation Successful Reference ID: 9bac803e-b507-49b7-ac7c-d8e8e3f3aa89">
<COUNT Records="9506" />
<DELIMITER value="09" />
<COLUMNS>   sysid   1   2   3   4   5   6   </COLUMNS>
<DATA>  252370080   Residential 0.160   No  ADDR0   06051</DATA>
<DATA>  252370081   Residential 0.440   Yes ADDR0   06043</DATA>
<DATA>  252370082   Residential 1.010   No  ADDR0   06023</DATA>
<DATA>More tab delimited text</DATA>
<!-- snip 9000+ lines -->
</RETS>
Run Code Online (Sandbox Code Playgroud)

我下载了一个响应的示例文件(大约22MB),这里是我调试和理智的结果.两台服务器都运行PHP版本5.3.8,但请注意不同的结果.我尽可能肯定两个文件是相同的(我想不同的文件大小,strlen和最后50个字符可以用具有额外回车字符的Windows换行符来解释).测试脚本:

error_reporting(-1);
ini_set('display_errors', 1);
$file = 'error-example.xml';
$xml = file_get_contents($file);

echo 'filesize:              ';
var_dump(filesize($file));

echo 'strlen:                ';
var_dump(strlen($xml));

echo 'simplexml object?      '; …
Run Code Online (Sandbox Code Playgroud)

php xml simplexml

15
推荐指数
1
解决办法
1万
查看次数

如何在格式错误的数据上关闭simpleXML?

我有一个XML设置,我试图使用simplexml加载.XML设置可以由Web应用程序的用户编辑.我想自己处理错误并向用户界面发送警告消息.但是,simplexml不断向格式错误的XML发出警告,而不是静静地返回false.

如何让simpleXML闭嘴而不是发出警告?那有选择吗?

php simplexml

13
推荐指数
1
解决办法
5444
查看次数

PHP的SimpleXML:如何在名称中使用冒号

我正在尝试使用SimpleXML生成RSS Google Merchant.

Google提供的示例是:

<?xml version="1.0"?>
<rss version="2.0" 
xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>The name of your data feed</title>
<link>http://www.example.com</link>
<description>A description of your content</description>
<item>
<title>Red wool sweater</title>
<link> http://www.example.com/item1-info-page.html</link>
<description>Comfortable and soft, this sweater will keep you warm on those cold winter nights.</description>
<g:image_link>http://www.example.com/image1.jpg</g:image_link> <g:price>25</g:price> <g:condition>new</g:condition> <g:id>1a</g:id>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)

我的代码有:

$product->addChild("g:condition", 'new');
Run Code Online (Sandbox Code Playgroud)

哪个产生:

<condition>new</condition>
Run Code Online (Sandbox Code Playgroud)

我在网上看到我应该使用:

$product->addChild("g:condition", 'new', 'http://base.google.com/ns/1.0');
Run Code Online (Sandbox Code Playgroud)

现在生成:

<g:condition xmlns:g="http://base.google.com/ns/1.0">new</g:condition>
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎是非常反直觉的,因为现在"xmlns"声明几乎在我的RSS feed中的每一行都只有一次在根元素中.

我错过了什么吗?

php xml rss simplexml

13
推荐指数
3
解决办法
9328
查看次数

SimpleXML基于属性值获取元素内容

我正在尝试根据属性的值访问元素的内容.使用PHP SimpleXML.我有以下XML设置:

<DocSum> 
    <Id>21242919</Id> 
    <Item Name="Author" Type="String">Nguyen T</Item>
    <Item Name="Title" Type="String">[Hemoptysis and spontaneous rupture of a primary renal angiosarcoma: a case report.]</Item>
</DocSum>
<DocSum> 
    <Id>21242919</Id> 
    <Item Name="Author" Type="String">Oliveira GC</Item>
    <Item Name="Title" Type="String">Disclosing ambiguous gene aliases by automatic literature profiling.</Item>
</DocSum>
<DocSum> 
    <Id>21242919</Id> 
    <Item Name="Author" Type="String">Vanderwall DE</Item>
    <Item Name="Title" Type="String">Metformin and digestive disorders.</Item>
</DocSum>
Run Code Online (Sandbox Code Playgroud)

这些是书.在这种情况下,我正试图获得标题.到目前为止我所拥有的是:

$xml = simplexml_load_file(url);
$docs = $xml->DocSum;
foreach($docs as $book){
        // Each book individual
}
Run Code Online (Sandbox Code Playgroud)

评论的地方我尝试了很多东西.

php simplexml css-selectors

13
推荐指数
2
解决办法
2万
查看次数

使用PHP更新XML节点

我有一个XML文件test.xml

<?xml version="1.0"?>
<info>
  <user>
    <name>
      <firstname>FirstName</firstname>
      <lastname>Last Name</lastname>
      <nameCoordinate>
        <xName>125</xName>
        <yName>20</yName>
      </nameCoordinate>
    </name>
  </user>
</info>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用PHP在表单提交上更新节点xName和yName.所以,我使用simplexml_load_file()加载了文件.PHP表单操作代码如下

<?php 
    $xPostName = $_POST['xName'];
    $yPostName = $_POST['yName'];

    //load xml file to edit
        $xml = simplexml_load_file('test.xml');

    $xml->info->user->name->nameCoordinate->xName = $xPostName;
    $xml->info->user->name->nameCoordinate->yName = $yPostName;
    echo "done";
?>
Run Code Online (Sandbox Code Playgroud)

我想更新节点值,但上面的代码似乎不正确.任何人都可以帮我纠正吗?

更新:我的问题有点类似于使用PHP更新XML文件但在这里,我从外部文件加载XML,我也在更新元素,而不是属性.这就是我的困惑所在.

php xml simplexml

13
推荐指数
1
解决办法
3万
查看次数

PHP simplexml:为什么xpath停止工作?

供应商稍微更改了XML标题后发生了一件奇怪的事情.我曾经能够使用xpath读取内容,但现在我甚至无法得到回复

$xml->xpath('/');
Run Code Online (Sandbox Code Playgroud)

他们改变了这个......

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE NewsML SYSTEM "http://www.newsml.org/dl.php?fn=NewsML/1.2/specification/NewsML_1.2.dtd" [
<!ENTITY % nitf SYSTEM "http://www.nitf.org/IPTC/NITF/3.4/specification/dtd/nitf-3-4.dtd">
%nitf;
]>
<NewsML>
...
Run Code Online (Sandbox Code Playgroud)

对此:

<?xml version="1.0" encoding="iso-8859-1"?>
<NewsML
  xmlns="http://iptc.org/std/NewsML/2003-10-10/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://iptc.org/std/NewsML/2003-10-10/ http://www.iptc.org/std/NewsML/1.2/specification/NewsML_1.2.xsd http://iptc.org/std/NITF/2006-10-18/   http://contentdienst.pressetext.com/misc/nitf-3-4.xsd"
>
...
Run Code Online (Sandbox Code Playgroud)

php xml xpath simplexml

12
推荐指数
1
解决办法
5991
查看次数

Simplexml_load_string($ string)返回一个空对象,但$ string包含xml?代码如下

我使用xml格式的cURL检索一些信息.

....

$xml = curl_exec($ch);

$data = simplexml_load_string($xml);
print_r($data);
//out put - SimpleXMLElement Object ( ) 
Run Code Online (Sandbox Code Playgroud)

如果我尝试 - print_r($xml);并查看页面源我得到

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ns7:users xmlns="http://www.example.com/xml/ns/rs" 
        xmlns:ns2="http://www.example.com/xml/ns/users" 
        xmlns:ns3="http://www.example.com/2004/11/tHistory" 
        xmlns:ns4="http://www.example.com/fsi/tHistory" 
        xmlns:ns5="http://www.example.com/2005/10/tHistory" 
        xmlns:ns6="http://www.example.com/2010/03/cs" 
        xmlns:ns7="http://www.example.com/2005/10/users" 
        xmlns:ns8="http://www.example.com/2010/03/tHistory">
    <ns7:user><ns7:id>Matt.Smith</ns7:id>
    <ns7:lastName>Smith</ns7:lastName>
    <ns7:firstName>Matt</ns7:firstName>
    <ns7:otherName></ns7:otherName>
    <ns7:gender>male</ns7:gender>
    <ns7:email>matt@company.co.uk</ns7:email>
    <ns7:locale>en</ns7:locale>
    <ns7:role><ns7:id>A</ns7:id>
    <ns7:name>System Administrator</ns7:name></ns7:role>
    <ns7:employeeNumber></ns7:employeeNumber>
    <ns7:organization>
        <ns7:id>8000</ns7:id>
        <ns7:name>Organisation Title</ns7:name>
    </ns7:organization>
    <ns7:organization>
        <ns7:id>20707</ns7:id>
        <ns7:name>London Office</ns7:name>
    </ns7:organization>
    <ns7:attribute>
        <ns7:code>0</ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code>0</ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description></ns7:attribute>
        <ns7:attribute><ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
        </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute> …
Run Code Online (Sandbox Code Playgroud)

php xml simplexml

12
推荐指数
2
解决办法
2万
查看次数

如何防止php simplexml中的自闭标签

我想使用php simplexml生成xml.

$xml = new SimpleXMLElement('<xml/>');

$output = $xml->addChild('child1');
$output->addChild('child2', "value");
$output->addChild('noValue', '');

Header('Content-type: text/xml');
print($xml->asXML());
Run Code Online (Sandbox Code Playgroud)

输出是

<xml>
   <child1>
      <child2>value</child2>
      <noValue/>
   </child1>
</xml>
Run Code Online (Sandbox Code Playgroud)

我想要的是如果标签没有值,它应该像这样显示

<noValue></noValue>
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用LIBXML_NOEMPTYTAGSimpleXML中关闭自动关闭的标签为PHP?

我试过了$xml = new SimpleXMLElement('<xml/>', LIBXML_NOEMPTYTAG);,但是没用.所以我不知道该放在哪里LIBXML_NOEMPTYTAG

php xml simplexml

12
推荐指数
2
解决办法
9733
查看次数

标签 统计

php ×10

simplexml ×10

xml ×6

class ×1

css-selectors ×1

namespaces ×1

rss ×1

wordpress ×1

xpath ×1