How can parse XML with this structure :
<sdk:sdk-repository xmlns:sdk="http://schemas.android.com/sdk/android/repository/7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
Generated on Thu Oct 22 10:16:34 PDT 2009 using eclair-sdk 17704: Platform. Addon. Tools. Doc.
-->
<sdk:platform>
<sdk:version>2.0</sdk:version>
<sdk:api-level>5</sdk:api-level>
<sdk:codename/>
<sdk:revision>01</sdk:revision>
<sdk:min-tools-rev>
<sdk:major>3</sdk:major>
</sdk:min-tools-rev>
<sdk:description>Android SDK Platform 2.0, revision 1</sdk:description>
<sdk:desc-url>http://developer.android.com/sdk/android-2.0.html</sdk:desc-url>
<sdk:obsolete/>
<sdk:archives>
<sdk:archive arch="any" os="linux">
<sdk:size>75095268</sdk:size>
<sdk:checksum type="sha1">be9be6a99ca32875c96ec7f91160ca9fce7e3c7d</sdk:checksum>
<sdk:url>android-2.0_r01-linux.zip</sdk:url>
</sdk:archive>
<sdk:archive arch="any" os="macosx">
<sdk:size>74956356</sdk:size>
<sdk:checksum type="sha1">2a866d0870dbba18e0503cd41e5fae988a21b314</sdk:checksum>
<sdk:url>android-2.0_r01-macosx.zip</sdk:url>
</sdk:archive>
<sdk:archive arch="any" os="windows">
<sdk:size>76288040</sdk:size>
<sdk:checksum type="sha1">aeb623217ff88b87216d6eb7dbc846ed53f68f57</sdk:checksum>
<sdk:url>android-2.0_r01-windows.zip</sdk:url>
</sdk:archive>
</sdk:archives>
<sdk:layoutlib>
<sdk:api>4</sdk:api>
</sdk:layoutlib>
<sdk:uses-license ref="android-sdk-license"/>
</sdk:platform>
...
how can get Version? and size?
and even get Generated date ?( on Thu Oct 22 10:16:34 PDT 2009 using eclair-sdk 17704: Platform. Addon. Tools. Doc. ) for each SDK Platform(sdk:platform)
very very TNX
Parsing the XML and parsing the comment in the file are two completely separate things.
Regarding the XML itself, you can use PHP's SimpleXml to parse it, but you'll have to remember to specify the namespace when accessing the children-elements.
We know we want namespaces, and we know which one we want, because each node that we're interested in is prefixed with something:. For example:
<sdk:version>2.0</sdk:version>
So, in this case, we are interested in the sdk namespace. You can find where the namespaces are defined in the top-most declaration node:
<sdk:sdk-repository xmlns:sdk="http://schemas.android.com/sdk/android/repository/7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
This declares the sdk namespace to be http://schemas.android.com/sdk/android/repository/7. The 7 is a version number, so keep this in mind if you ever need to update to use different SDKs.
A small example of parsing the XML, using your sample XML, would be:
// load the file and create the XML element
$feed = file_get_contents("repository.xml");
$xml = new SimpleXmlElement($feed);
// define the namespace you need to use (we want xmlns:sdk)
$sdk = $xml->children("http://schemas.android.com/sdk/android/repository/7");
// get the "platform" root element and start using it's nodes
$platform = $sdk->platform;
echo 'Version: ' . $platform->version . "<br />";
echo 'Description: ' . $platform->description . "<br />";
foreach ($platform->archives->archive as $archive) {
echo 'Archive: ' . $archive->url . ", size: " . $archive->size . "<br />";
}
To match the Generated on ... line, you'll either need to use substring parsing or build a regex that can parse it. I'm opting for the latter option to save lines-of-code, but it's really up to you how you'd prefer to go about it.
If you wanted to use a regex, and this is very-specific to the output in your sample XML so you may need to adjust it to be more dynamic, you can use PHP's preg_match and a very verbose pattern:
$pattern = '/<!--[\r\n\s]*Generated on (.*) using eclair-sdk 17704: Platform\. Addon\. Tools\. Doc\.[\r\n\s]*-->/';
$comments = array();
preg_match($pattern, $feed, $comments);
echo $comments[1];
Output:
Thu Oct 22 10:16:34 PDT 2009
Related
I have the following code excerpt
$sgn=$creditInvoice ? 1.0 : -1.0;
$net=$sgn*$s->totalNetValue;$vat=$sgn*$s->totalVatAmount;
echo "net=$net vat=$vat s->totalNetValue=$s->totalNetValue s->totalVatAmount=$s->totalVatAmount gettype(s->totalNetValue)=".gettype($s->totalNetValue)."<br>";
The output is unexpectedly (to me)
net=-28 vat=-6 s->totalNetValue=28.32 s->totalVatAmount=6.80
gettype(s->totalNetValue)=object
The data has been read by simplexml_load_string so it has to do with that.
If a cast the $s->totalNetValue to float or double it works correctly.
What should I do to write "correct but not redundant code" ?
Bellow the complete code to reproduce:
<?php
$x='<?xml version="1.0" encoding="utf-8"?> <invoice> <invoiceSummary> <totalNetValue>20.12</totalNetValue> <totalVatAmount>4.83</totalVatAmount> </invoiceSummary> </invoice>';
$i = simplexml_load_string($x);
print_r($i);echo '<br>';
$s = $i->invoiceSummary;
print_r($s);echo '<br>';
$net=1.0*$s->totalNetValue;
$vat=1.0*$s->totalVatAmount;
echo "net=$net vat=$vat s->totalNetValue=$s->totalNetValue s->totalVatAmount=$s->totalVatAmount gettype(s->totalNetValue)=".gettype($s->totalNetValue)."<br>";
?>
Okay, coffee finally kicked in an I'm seeing what everyone else was seeing.
Prior to PHP 7.3, when SimpleXML objects were used in a mathematical context they were always treated as ints. This change is documented here.
And you can see the old way here and the new way here
I am having an issue wrapping my head around the SimpleXML syntax.
I am trying to echo a few pieces of info I pull out of a larger xml document based on a predefined value DELN:
$mytimes= simplexml_load_file('http://www.bart.gov/dev/eta/bart_eta.xml');
$freemont = $mytimes->station->abbr[DELN]->eta->destination[freemont]->estimate;
$millbrae = $mytimes->station->abbr[DELN]->eta->destination[millbrae]->estimate;
$richmond = $mytimes->station->abbr[DELN]->eta->destination[richmond]->estimate;
My XML:
<station>
<name>El Cerrito del Norte</name>
<abbr>DELN</abbr>
<date>11/30/2012</date>
<time>07:41:02 AM PST</time>
<eta>
<destination>Fremont</destination>
<estimate>14 min, 29 min, 44 min</estimate>
</eta>
<eta>
<destination>Millbrae</destination>
<estimate>6 min, 21 min, 36 min</estimate>
</eta>
<eta>
<destination>Richmond</destination>
<estimate>4 min, 10 min, 17 min</estimate>
</eta>
</station>
Let me know if I need to provide more info. Thanks.
First - I suppose you're not defining DELN constant, probably you should use a string ('DELN'). Same with freemont, millbrae and richmont. The problem is not only SimpleXML syntax, basically its misunderstanding of XML structure that you want to read (abbr is not in straight path with eta, same with destination and estimate). Anyway - xpath will be your friend here:
$fremont = $mytimes->xpath('//station/eta/estimate[../../abbr/text()=\'DELN\' and ../destination/text()=\'Fremont\']');
$millbrae = $mytimes->xpath('//station/eta/estimate[../../abbr/text()=\'DELN\' and ../destination/text()=\'Millbrae\']');
$richmond = $mytimes->xpath('//station/eta/estimate[../../abbr/text()=\'DELN\' and ../destination/text()=\'Richmond\']');
then you can do:
foreach ($fremont as $value) echo $value;
or just
if (count($fremont) > 0) echo $fremont[0];
I have the following code...
<?php
include("lib/xmlrpc.inc");
$email='whatever#hotmail.com';
$c=new xmlrpc_client("/register/index.php", "ws.myserver.com", 80);
$f=new xmlrpcmsg('existsEmail', array(new xmlrpcval($email, "base64")));
print "<pre>" . htmlentities($f->serialize( )) . "</pre>";
$r=$c->send($f);
$v=$r->value( );
if (!$r->faultCode( )) {
print "Email is". $email . " is " .
$v->scalarval( ) . "<br />";
print "<hr />I got this value back<br /><pre>" .
htmlentities($r->serialize( )). "</pre><hr />\n";
} else {
print "Fault: ";
print "Code: " . $r->faultCode( ) .
" Reason '" .$r->faultString( )."'<br />";
}
?>
I need to consume the WebService located at http://ws.myserver.com/register/index.php.
I pass the email as a parameter and then the XMLRPC.inc library will encode it using base64.
I've got a good XML request shown below:
<?xml version="1.0"?>
<methodCall>
<methodName>existsEmail</methodName>
<params>
<param>
<value><base64>dnJvZHJpZ3VlekBpY2NrLm5ldC5jbw==</base64></value>
</param>
</params>
</methodCall>
BUUUT, when I tried to get a response from the server I've to the following error
Fault: Code: -32601 Reason 'server error. requested method not found'
Any ideas? I'm getting crazy about how to call the existsEmail method from my PHP code...I'm sure it is there but I don't know if I'm missing something..
You are getting an error message (Specification for Fault Code Interoperability, version 20010516) from the XMLRPC endpoint you're communicating with.
It is a defined error code:
-32601 ---> server error. requested method not found
The RPC method you requested was not found by the server. Contact the support of the service you consume to get a list of all available methods. If that method should be available, contact the support and discuss the issue with them.
You asked in comment:
Is there any way [to] verify which methods are available?
That depends on the service. XMLRPC on sourceforge has a suggestion of defined methods you can call to list information about the functions available:
XML-RPC Introspection
system.listMethods
system.methodSignature
system.methodHelp
You can try if it works with your service, too. AFAIK those are common, I wrapped up a quick example, you find the full code below. See the output below the code as well.
$path = 'http://xmlrpc-c.sourceforge.net/api/sample.php';
printf("\n XMLRPC Service Discovery\n\n for: '%s'\n\n", $path);
$discovery = new Discovery($path);
$methods = $discovery->getMethods();
printf(" Method Summary:\n ===============\n", count($methods));
foreach ($methods as $i => $method)
{
printf(" %'.-2d %s\n", $i + 1, $method->getName());
}
printf("\n Method Details (%d):\n ===================\n", count($methods));
foreach ($methods as $i => $method)
{
printf(" %'.-2d %s\n", $i + 1, $method->getName());
printf("\n %s\n", $method);
printf("\n%s\n\n", preg_replace('/^/um', ' ', wordwrap($method->getHelp(), 46)));
}
Output:
XMLRPC Service Discovery
for: 'http://xmlrpc-c.sourceforge.net/api/sample.php'
Method Summary:
===============
1. debug.authInfo
2. sample.add
3. sample.sumAndDifference
4. system.listMethods
5. system.methodHelp
6. system.methodSignature
Method Details (6):
===================
1. debug.authInfo
<struct> debug.authInfo
Report any HTTP authentication in use
2. sample.add
<int> sample.add (<int>, <int>)
Add two numbers
3. sample.sumAndDifference
<struct> sample.sumAndDifference (<int>, <int>)
Add and subtract two numbers
4. system.listMethods
<array> system.listMethods (<string>)
This method lists all the methods that the
XML-RPC server knows how to dispatch
5. system.methodHelp
<string> system.methodHelp (<string>)
Returns help text if defined for the method
passed, otherwise returns an empty string
6. system.methodSignature
<array> system.methodSignature (<string>)
Returns an array of known signatures (an array
of arrays) for the method name passed. If no
signatures are known, returns a none-array
(test for type != array to detect missing
signature)
You can find the sourcecode here: XMLRPC Discovery Service
I am currently trying to display a RSS feed in a PHP page, but it seems to have a problem between the xslt_create() function and PHP5.
<?php
$xh = xslt_create();
$file=fopen(WEB_DIR . 'assets/_xml/rss.xml','r');
$xml=fread($file,16384);
fclose($file);
$file=fopen(WEB_DIR . 'assets/_xml/rss.xslt','r');
$xsl=fread($file,16384);
fclose($file);
$arguments = array(
'/_xml' => $xml,
'/_xsl' => $xsl
);
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
xslt_free($xh);
print "$result";
?>
I get the error
Fatal error: Call to undefined
function xslt_create() in
What do I have to do to correct this issue ?
xslt_create only exists in PHP 4 - as this approach to parsing XML was removed in PHP 5.
As such, I'd recommend updating your code to use one of the current PHP 5 approaches as listed within the XML Manipulation section of the manual. (The XSL extension provides a XSLTProcessor class that is probably the nearest direct equivalent.)
Is there any available solution for (re-)generating PHP code from the Parser Tokens returned by token_get_all? Other solutions for generating PHP code are welcome as well, preferably with the associated lexer/parser (if any).
From my comment:
Does anyone see a potential problem,
if I simply write a large switch
statement to convert tokens back to
their string representations (i.e.
T_DO to 'do'), map that over the
tokens, join with spaces, and look for
some sort of PHP code pretty-printing
solution?
After some looking, I found a PHP homemade solution in this question, that actually uses the PHP Tokenizer interface, as well as some PHP code formatting tools which are more configurable (but would require the solution as described above).
These could be used to quickly realize a solution. I'll post back here when I find some time to cook this up.
Solution with PHP_Beautifier
This is the quick solution I cooked up, I'll leave it here as part of the question. Note that it requires you to break open the PHP_Beautifier class, by changing everything (probably not everything, but this is easier) that is private to protected, to allow you to actually use the internal workings of PHP_Beautifier (otherwise it was impossible to reuse the functionality of PHP_Beautifier without reimplementing half their code).
An example usage of the class would be:
file: main.php
<?php
// read some PHP code (the file itself will do)
$phpCode = file_get_contents(__FILE__);
// create a new instance of PHP2PHP
$php2php = new PHP2PHP();
// tokenize the code (forwards to token_get_all)
$phpCode = $php2php->php2token($phpCode);
// print the tokens, in some way
echo join(' ', array_map(function($token) {
return (is_array($token))
? ($token[0] === T_WHITESPACE)
? ($token[1] === "\n")
? "\n"
: ''
: token_name($token[0])
: $token;
}, $phpCode));
// transform the tokens back into legible PHP code
$phpCode = $php2php->token2php($phpCode);
?>
As PHP2PHP extends PHP_Beautifier, it allows for the same fine-tuning under the same API that PHP_Beautifier uses. The class itself is:
file: PHP2PHP.php
class PHP2PHP extends PHP_Beautifier {
function php2token($phpCode) {
return token_get_all($phpCode);
}
function token2php(array $phpToken) {
// prepare properties
$this->resetProperties();
$this->aTokens = $phpToken;
$iTotal = count($this->aTokens);
$iPrevAssoc = false;
// send a signal to the filter, announcing the init of the processing of a file
foreach($this->aFilters as $oFilter)
$oFilter->preProcess();
for ($this->iCount = 0;
$this->iCount < $iTotal;
$this->iCount++) {
$aCurrentToken = $this->aTokens[$this->iCount];
if (is_string($aCurrentToken))
$aCurrentToken = array(
0 => $aCurrentToken,
1 => $aCurrentToken
);
// ArrayNested->off();
$sTextLog = PHP_Beautifier_Common::wsToString($aCurrentToken[1]);
// ArrayNested->on();
$sTokenName = (is_numeric($aCurrentToken[0])) ? token_name($aCurrentToken[0]) : '';
$this->oLog->log("Token:" . $sTokenName . "[" . $sTextLog . "]", PEAR_LOG_DEBUG);
$this->controlToken($aCurrentToken);
$iFirstOut = count($this->aOut); //5
$bError = false;
$this->aCurrentToken = $aCurrentToken;
if ($this->bBeautify) {
foreach($this->aFilters as $oFilter) {
$bError = true;
if ($oFilter->handleToken($this->aCurrentToken) !== FALSE) {
$this->oLog->log('Filter:' . $oFilter->getName() , PEAR_LOG_DEBUG);
$bError = false;
break;
}
}
} else {
$this->add($aCurrentToken[1]);
}
$this->controlTokenPost($aCurrentToken);
$iLastOut = count($this->aOut);
// set the assoc
if (($iLastOut-$iFirstOut) > 0) {
$this->aAssocs[$this->iCount] = array(
'offset' => $iFirstOut
);
if ($iPrevAssoc !== FALSE)
$this->aAssocs[$iPrevAssoc]['length'] = $iFirstOut-$this->aAssocs[$iPrevAssoc]['offset'];
$iPrevAssoc = $this->iCount;
}
if ($bError)
throw new Exception("Can'process token: " . var_dump($aCurrentToken));
} // ~for
// generate the last assoc
if (count($this->aOut) == 0)
throw new Exception("Nothing on output!");
$this->aAssocs[$iPrevAssoc]['length'] = (count($this->aOut) -1) - $this->aAssocs[$iPrevAssoc]['offset'];
// post-processing
foreach($this->aFilters as $oFilter)
$oFilter->postProcess();
return $this->get();
}
}
?>
In the category of "other solutions", you could try PHP Parser.
The parser turns PHP source code into an abstract syntax tree....Additionally, you can convert a syntax tree back to PHP code.
If I'm not mistaken http://pear.php.net/package/PHP_Beautifier uses token_get_all() and then rewrites the stream. It uses heaps of methods like t_else and t_close_brace to output each token. Maybe you can hijack this for simplicity.
See our PHP Front End. It is a full PHP parser, automatically building ASTs, and a matching prettyprinter that regenerates compilable PHP code complete with the original commments. (EDIT 12/2011:
See this SO answer for more details on what it takes to prettyprint from ASTs, which are just an organized version of the tokens: https://stackoverflow.com/a/5834775/120163)
The front end is built on top of our DMS Software Reengineering Toolkit, enabling the analysis and transformation of PHP ASTs (and then via the prettyprinter code).