John Davidson

editing xml file node values and saving as XML string to upload to webservice in PHP

0 comments
Message:


I have a basic XML file that contains the following data:


<?xml version="1.0"?>
<rosters id_parent_provider="928" upload_key="8257QP01">
<roster>
<id_provider>928</id_provider>
<provider_course_code>2022 - Medical Errors</provider_course_code>
<id_course>895456</id_course>
<id_publishing></id_publishing>
<end_date></end_date>
<attendees>
<attendee>
<licensee_profession>AAA</licensee_profession>
<licensee_number>BBB123</licensee_number>
<first_name></first_name>
<last_name></last_name>
<cebroker_state>CC</cebroker_state>
<date_completed>DD-DD-DDDD</date_completed>
<ce_credit_hours>E</ce_credit_hours>
</attendee>
</attendees>
</roster>
</rosters>

I load the file in using:


$xml_string = file_get_contents($test_file);
$roster = simplexml_load_string($xml_string);

and then roster contains the following upon var_dump:


object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(2) { ["id_parent_provider"]=> string(3) "928" ["upload_key"]=> string(8) "8257QP01" } ["roster"]=> object(SimpleXMLElement)#5 (6) { ["id_provider"]=> string(3) "928" ["provider_course_code"]=> object(SimpleXMLElement)#6 (0) { } ["id_course"]=> string(6) "895456" ["id_publishing"]=> object(SimpleXMLElement)#7 (0) { } ["end_date"]=> object(SimpleXMLElement)#8 (0) { } ["attendees"]=> object(SimpleXMLElement)#9 (1) { ["attendee"]=> object(SimpleXMLElement)#10 (7) { ["licensee_profession"]=> string(3) "AAA" ["licensee_number"]=> string(7) "BBB1234" ["first_name"]=> object(SimpleXMLElement)#11 (0) { } ["last_name"]=> object(SimpleXMLElement)#12 (0) { } ["cebroker_state"]=> string(2) "CC" object(SimpleXMLElement)#13 (0) { } ["date_completed"]=> string(10) "DD-DD-DDDD" ["ce_credit_hours"]=> string(1) "E" } } } } 

I need to replace text AAA, BB1234, CC, DD-DD-DDDD and E with values I have stored in my website's database. How do I access the appropriate nodes and then change the value. I have tried following several simpleXMLElement tutorials, but not sure what i am doing wrong.



My Answer:
xml


John Doe
johndoe@example.com
30



To edit the values of the nodes in the XML file and save it as an XML string to upload to a web service in PHP, you can use the SimpleXMLElement class provided by PHP. Here's an example code snippet to achieve this:

php

// Load the XML file
$xmlString = file_get_contents('data.xml');
$xml = new SimpleXMLElement($xmlString);

// Update the node values
$xml->name = 'Jane Smith';
$xml->email = 'janesmith@example.com';
$xml->age = 25;

// Save the updated XML as a string
$updatedXmlString = $xml->asXML();

// Upload the updated XML string to a web service
// You can use cURL or any other method to upload the XML string

echo $updatedXmlString;

?>


In this code snippet, we first load the XML file using the file_get_contents function and create a SimpleXMLElement object from it. We then update the values of the nodes using the object properties. Finally, we save the updated XML as a string using the asXML method and echo it out.

You can then use this updated XML string to upload to a web service using cURL or any other method of your choice.

Rate this post

5 of 5 based on 6110 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us