我是PHP新手,这可能是一个愚蠢的问题,所以不要因为我不理解某事而投票给我...
php -r "print_r(simplexml_load_file('http://twitter.com/statuses/user_timeline/31139114.rss'));"
Run Code Online (Sandbox Code Playgroud)
让我们以此为例,通过运行此命令,我得到(在屏幕上)XML输出.
我的问题是可以保存这些数据,而不仅仅是屏幕,但是在一个文件中,然后读取该文件并完全相同,就好像你已经使那个simplexml_load_file()
我知道如果XML格式是如何使用simplexml_load_file来获取XML结果
<bowlcontents>
<banana>yellow</banana>
<apple>red</apple>
</bowlcontents>
Run Code Online (Sandbox Code Playgroud)
但是,我有一些格式的代码
<bowlcontents>
<fruit type="banana" skin="yellow" />
<fruit type="apple" skin="red" />
</bowlcontents>
Run Code Online (Sandbox Code Playgroud)
我想以与第一个例子相同的方式操纵它.我该怎么做?
编辑:这正是我想要做的,但下面的代码不起作用.
<?php
$url = "http://worldsfirstfruitAPI.com/fruit.xml";
$xml = (simplexml_load_file($url));
$results = array();
foreach ($xml->bowlcontents->fruit as $fruit) {
$results[] = array(
$fruit['type'] => $fruit['skin'],
);
}
return $results;
}
?>
Run Code Online (Sandbox Code Playgroud)
所以在它的最后我想有一个数组,键=值:
香蕉=黄色
苹果=红
...
我希望这澄清一下.谢谢!
我这样做了
<blink>
$xml = file_get_contents(http://weather.yahooapis.com/forecastrss?w=12797541);
$yahoo_response = new SimpleXMLElement($xml , 0, true);
</blink>
Run Code Online (Sandbox Code Playgroud)
我得到了一个像这样的XML解析警告:
PHP Warning: SimpleXMLElement::__construct()
[<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]:
I/O warning : failed to load external entity "<?xml version="1.0"
Run Code Online (Sandbox Code Playgroud)
.....
消息的一个重要部分是:
I/O warning : failed to load external entity
Run Code Online (Sandbox Code Playgroud)
我无法用这一行解析任何东西:
echo (string) $yahoo_response->rss->channel->item->title;
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题或绕过它?
谢谢,亚历克斯
这是错误:
[01-Sep-2011 08:15:01] PHP警告:SimpleXMLElement :: __ construct()[<ah ref ='simplexmlelement .-- construct'> simplexmlelement .-- construct]:../ domain /feed-yself- tips.php:1:解析器错误:预期开始标记,第135行/home/domain/public_html/mergedrss.php中找不到"<"
[01-Sep-2011 08:15:01] PHP警告:SimpleXMLElement :: __ construct()[<ah ref ='simplexmlelement .-- construct'> simplexmlelement .-- construct]:1f45 in/home/domain/public_html /第135行的mergedrss.php
[01-Sep-2011 08:15:01] PHP警告:SimpleXMLElement :: __ construct()[<ah ref ='simplexmlelement .-- construct'> simplexmlelement .-- construct]:^ in/home/domain/public_html /第135行的mergedrss.php
[01-Sep-2011 08:15:01] PHP致命错误:在/home/domain/public_html/mergedrss.php:135中,未捕获异常'Exception',消息'String无法解析为XML'
堆栈跟踪:
#0 /home/domain/public_html/mergedrss.php(135): SimpleXMLElement->__construct('.../domain...', 0, true)
#1 /home/domain/public_html/mergedrss.php(57): MergedRSS->__fetch_rss_from_url('.../domain...')
#2 /home/domain/public_html/feed.php(21): MergedRSS->export(false, true, 15)
#3 {main}
thrown in /home/domain/public_html/mergedrss.php on line 135
以下格式是否适用于XML?我正在尝试使用PHP解析文件simplexml_load_file但仍然收到错误,指出在第3行的文档末尾有"额外内容".
这是XML文件:
<?xml version="1.0"?>
<title>Test File</title>
<data>
<record id="1">
<department>
<name>ACME</name>
<number>5</number>
</dep_name>
<floor>
<name>ACME Floor</name>
<number>5</number>
</floor>
</record>
</data>
Run Code Online (Sandbox Code Playgroud) 如何使用 PHP 从 HTML 表单读取 XML 文件的内容而不上传文件?这是我的表格:
<form action="readxml.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
我想使用 SimpleXML 处理用户选择的 XML,将其值存储在数据库中,因此我不需要实际上传文件。
谢谢!
假设我有一个如下的 XML -
$xml = '<?xml version="1.0"?>
<step number="9">
<s_name>test</s_name>
<b_sel>12345</b_sel>
<b_ind>7</b_ind>
</step>';
Run Code Online (Sandbox Code Playgroud)
我希望将其转换为对象,但是当我执行以下步骤时,它给了我如下的 stdclass 对象 [我将它分配给 $stepInformation 变量] -
$xml = json_decode(json_encode((array) simplexml_load_string($xml)), 1);
$stepInformation = stdClass Object
(
[@attributes] => Array
(
[number] => 9
)
[s_name] => test
[b_sel] => 12345
[b_ind] => 7
)
Run Code Online (Sandbox Code Playgroud)
所以当我在 php 函数中解析这个 stdclass 对象时
function convertStepInformationToArray($stepInformation)
{
$dom = new DOMDocument();
$stepInfo = "{$stepInformation->s_name}{$stepInformation->b_sel}{$stepInformation->b_ind}";
$dom->loadXML("<document>" . $stepInfo . "</document>");
$domx = new DOMXPath($dom);
$entries = $domx->evaluate("//step");
return $entries;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是
DOMNodeList …Run Code Online (Sandbox Code Playgroud) namespaces尽管我尝试使用simpleXML 但仍然没有结果,但我不知道如何解析此 XML 响应...有什么想法吗?
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<ns3:GetDistrictByAddressResponse xmlns:ns3="http://il/co/bar/webservices/getdistrictbyaddress">
<TimeFrameTable>
<CustomerNumber>250</CustomerNumber>
<Row>
<WindowDate>10052016</WindowDate>
<WeekDay>Sunday</WeekDay>
<FromHour>1130</FromHour>
<ToHour>1430</ToHour>
</Row>
<Row>
<WindowDate>10052016</WindowDate>
<WeekDay>Sunday</WeekDay>
<FromHour>1430</FromHour>
<ToHour>1730</ToHour>
</Row>
</TimeFrameTable>
</ns3:GetDistrictByAddressResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud) 我想将我的 XML 对象转换为字符串格式。目前我有一个 XML 对象。我想将它连接到一个字符串。为了能够做到这一点,我想将 XML 对象转换为字符串。
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
<soapenv:Header />
<soapenv:Body>
<ser:processTrans>
<xmlValue>
<![CDATA[
<ebpacket>
<head>
<packettype> UserAuthorization</packettype>
<staffcode> ePay_UserName </staffcode>
<pwd> ePay_Password </pwd>
<env> ePay_Evnironment </env>
</head>
<body>
<email> _username </email>
<loginpwd> _password </loginpwd>
<deviceid> DeviceId </deviceid>
<tokenid> TokenId </tokenid>
<cip> CustomerIpAddress </cip>
</body>
</ebpacket>
]]>
</xmlValue>
</ser:processTrans>
Run Code Online (Sandbox Code Playgroud)
这是 simpleXML 对象。我想将其转换为字符串以对此执行字符串函数。
我需要 xml 节点的完整路径。我看到了这个问题
的答案,但我无法使用它。
下面是我在 php web 测试器上使用但没有成功的代码:
$xml = <<<EOF
<root>
<First>
<Martha>Text01</Martha>
<Lucy>Text02</Lucy>
<Bob>
<Jhon>Text03</Jhon>
</Bob>
<Frank>One</Frank>
<Jessy>Two</Jessy>
</First>
<Second>
<Mary>
<Jhon>Text04</Jhon>
<Frank>Text05</Frank>
<Jessy>Text06</Jessy>
</Mary>
</Second>
</root>
EOF;
$MyXml = new SimpleXMLElement($xml);
$Jhons = $MyXml->xpath('//Jhon');
foreach ($Jhons as $Jhon){
echo (string) $Jhon;
//No one of the following works
echo (string) $Jhon->xpath('./node()/path()');
echo (string) $Jhon->xpath('./path()');
echo (string) $Jhon->xpath('.path()');
echo (string) $Jhon->path();
echo '<br/> ';
}
Run Code Online (Sandbox Code Playgroud)
我需要:“/root/First/Bob/Jhon”和“/root/Second/Mary/Jhon”