I want to update to_status value.
I have following xml file.
<challenges>
<challenge>
<challenge_id>1385</challenge_id>
<debate_id>988</debate_id>
<comment_id>157</comment_id>
<from_id>42</from_id>
<to_id>3</to_id>
<from_status> true</from_status>
<to_status> false</to_status>
<timestamp>1320933898</timestamp>
</challenge>
</challenges>
How can i update it?
Look into CakePHP's "XML" class. In versions prior to 2.0, you have to "App::import('Xml')" the XML class but in 2.0 it is a core library.
With the XML class, you can render the XML as an array, manipulate it the way you want, and simply re-transform it into an XML string.
Check out these links:
CakePHP 1.3 XML : http://book.cakephp.org/view/1473/XML
CakePHP 2.0 XML : http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html?highlight=xml
Related
I am trying to parse this complex SOAP response:
But I get lost with the namespaces and children methods...any idea how to extract the highlighted data?
I tried this:
$xml->children( $ns['S'] )->Body->children( $ns['ns7'] )->Answer->children( $ns['ns3'] );
but it doesn't work
You should use a WSDL to PHP generator which will ease you the request construction and response handling. Indeed, you'll handle PHP object with getters/setters which is better in my point of view.
I can only suggest you to try the PackageGenerator project.
I am new to simplexml parser in PHP. I have run the examples that I have found and they work as advertised. I can't get it to work with my program though. I have searched in here for hours.
I have an XML file called core.xml (note that the node tags have colons in):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dc:title>Lecture 3</dc:title>
<dc:creator>John Brown & Greg Smith</dc:creator>
<cp:lastModifiedBy>Greg Smith</cp:lastModifiedBy>
<cp:revision>165</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2010-02-19T04:37:55Z</dcterms:created>
<dcterms:modified xsi:type="dcterms:W3CDTF">2014-01-30T02:41:30Z</dcterms:modified>
</cp:coreProperties>
I use following code to load it into the parser:
if (file_exists('core.xml')){
echo 'file exists <br>';
$xml_core = simplexml_load_file('core.xml');
print_r($xml_core);
}
else
{
exit('Failed to open core.xml.');
}
The file exists but all I get at the print_r is:
file exists
SimpleXMLElement Object ( )
How do I access the nodes? Other XML files that I have to use are many layers in depth.
Thanks
What you describe as an error pattern in your question is actually correct behaviour and not an error.
If you use print_r (or var_dump for that matter) on a SimpleXMLElement it will only show you some rudimentary information about that object and not the entire content of the XML document.
To see the whole XML, use the asXML() method instead. Alternatively to obtain debug information you can use a library that is specifically aware on how to debug SimpleXMLElement content's like simplexml_debug written by IMSop.
In your specific case, the object shows empty (no object members) because there aren't any nodes in the XML document within the default namespace (see: Namespaces in XML 1.0) that - after the more or less complex rules of SimpleXMLElement to array transitions were applied - would have been passed from the objects internal debug handler to the print_r or var_dump function to be displayed then. So only the class-name is given so that you can see it's an object of a certain class - and has no members:
SimpleXMLElement Object ( )
^ ^ ^
object type | `------ object fields (empty)
|
variable type
How to deal with that? It's very simple: Know the XML (you know it aleady) and obtain the data from it as it's documented:
PHP: Dealing with XML errors - Manual - Shows how you create (load) a SimpleXMLElement in a controlled manner and controlling error conditions.
PHP: Basic SimpleXML usage - Manual - Shows how to access data inside an XML document via the API of SimpleXMLElement.
As your XML element names contain colons, you should be made aware that these are so called XML namespaces. As you've seen differences in the print_r output, you will also see differences in accessing the child elements within that namespace. You have to use the children() method and specify the namespace of which you would like to obtain the child elements. If you use the xpath() method to access elements, you need to register an namespace prefix before you use it on the SimpleXMLElement you call that method on.
Some assorted selection of SimpleXML and XML Namespace related Q&A material here on site:
Parse XML with Namespace using SimpleXML (Feb 2009)
PHP namespace simplexml problems (Jan 2010)
PHP SimpleXML Namespace Problem (May 2011)
Parse XML namespaces with php SimpleXML (May 2013)
You can also obtain this information from the PHP manual, just take a look for namespace related information within the SimpleXMLElement API documentation.
You can use file_get_contents to load XML file.
Try
if(file_exists('core.xml'))
{
echo 'file exists <br>';
$xml_core = file_get_contents('core.xml');
$xml = new SimpleXMLElement($xml_core);
print_r($xml);
}
else
{
exit('Failed to open core.xml.');
}
I am calling a API method through cURL and I got this response:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo
xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<id>75080000002s5siAAA</id>
<operation>query</operation>
<object>User</object>
<createdById>00580000008ReolAAC</createdById>
<createdDate>2015-06-23T13:03:01.000Z</createdDate>
<systemModstamp>2015-06-23T13:03:01.000Z</systemModstamp>
<state>Open</state>
<concurrencyMode>Parallel</concurrencyMode>
<contentType>CSV</contentType>
<numberBatchesQueued>0</numberBatchesQueued>
<numberBatchesInProgress>0</numberBatchesInProgress>
<numberBatchesCompleted>0</numberBatchesCompleted>
<numberBatchesFailed>0</numberBatchesFailed>
<numberBatchesTotal>0</numberBatchesTotal>
<numberRecordsProcessed>0</numberRecordsProcessed>
<numberRetries>0</numberRetries>
<apiVersion>34.0</apiVersion>
<numberRecordsFailed>0</numberRecordsFailed>
<totalProcessingTime>0</totalProcessingTime>
<apiActiveProcessingTime>0</apiActiveProcessingTime>
<apexProcessingTime>0</apexProcessingTime>
</jobInfo>
I want to access|parse that result in a easy way and I don't know if I should deserializing the XML or just try to read it using some PHP native XML function. So ideas on this first doubt?
If it is better to deserialize the XML then I have read this post "Deserializing XML with JMSSerializerBundle in Symfony2" and is not clear at all for me if I will need an entity to achieve that. Also this other topic
and still confuse to me. Any advice on that? Experiences? Suggestions?
It depends on your intention. If you want to directly push part or all of the XML to an entity/document object for saving to a database then the JMSSerializerBundle can do this very smartly and is definitely the best way to do it.
If however you just want to extract one or two fields from the xml and use them in other business logic then simply loading the xml into a SimpleXML object is often simpler.
You can use any object (not only an entity) to deserialize the XML file to. It is recommended to deserialize to a object because you probably want to use it in an OOP way.
This is a well explained blog about JMS serializer (bundle) including a XML deserialization example in a user object: http://johnkary.net/blog/deserializing-xml-with-jms-serializer-bundle/
I am developing a PHP-powered application component which exports some data to an XML file which must follow a scheme defined by XSD file. I know how to validate the file manually, but it would be very handy if this could be done within unit tests.
Is there any library or framework available which can me help doing that?
One possibility would be to use DOMDocument::schemaValidate or DOMDocument::schemaValidateSource. Since either of those two methods returns a boolean value, you could then assert that the returned value is true.
This PHPUnit XSD validation lib defines a custom PHPUnit_Framework_Constraint uses DOMDocument::schemaValidate.
The advantage is that it gives a useful error when the validation fails, rather than just saying that 'false is not true'.
Usage
$constraint = new Jasny\PHPUnit\Constraint\XSDValidation("myschema.xsd");
$xml = $this->object->doSomething();
$this->assertThat($xml, $constraint);
Note: This is a shameless plug of my own open source library.
I'm using the native SOAP class in PHP 5, having changed from NuSOAP as the native class is faster (and NuSOAP development seems to have ceased). However the PHP 5 SOAP lacks the ability to generate WSDL.
Has anyone experience of generating WSDL in PHP? If so, please recommend your preferred method.
Thanks.
Stuart,
If you or anyone else is looking for a solution to this problem here's what I did.
First get this script: http://www.phpclasses.org/browse/download/zip/package/3509/name/php2wsdl-2009-05-15.zip
Then look at its example files. After that I just sliced it the way I needed because I'm using codeigniter:
function wsdl(){
error_reporting(0);
require_once(APPPATH."/libraries/WSDLCreator.php"); //Path to the library
$test = new WSDLCreator("Webservice", $this->site."/wsdl");
//$test->includeMethodsDocumentation(false);
$test->addFile(APPPATH."/controllers/gds.php");
$test->addURLToClass("GDS", $this->site);
$test->ignoreMethod(array("GDS"=>"GDS"));
$test->ignoreMethod(array("GDS"=>"accessCheck"));
$test->createWSDL();
$test->printWSDL(true); // print with headers
}
That it, your all done.
Btw, I'm using SoapServer and SoapClient in php5+
You can try these options:
- https://code.google.com/p/php-wsdl-creator/ (GPLv3)
- https://github.com/piotrooo/wsdl-creator/ (GPLv3)
- http://www.phpclasses.org/package/3509-PHP-Generate-WSDL-from-PHP-classes-code.html (BSD like)
The first one might be your best option if the licence fits your project.
Generating a WSDL on the fly is not something that happens very often - it would tend to raise a few questions about the stability of your service!
Zend Studio can generate a WSDL from a PHP class, and there are a few other similar tools.
If you do need to generate the WSDL dynamically, take a look at Zend Framework library: Zend_Soap_AutoDiscover
Zend_Soap_AutoDiscover is a good alternative to NuSOAP. But, you can also create the WSDL file from scratch which can be very complicated and error prone. To ease this process, you can use an IDE to generate the WSDL file for your PHP functions and pass it as a parameter to your PHP SoapServer class. Check out the complete tutorial on How to generate wsdl for php native soap class