RRO Posted February 10, 2022 Share Posted February 10, 2022 Hello, I’m trying to change an XML file by PHP. It’s working but I can’t find how I can select the specific ID. any ideas? For example, I would like to change the text of <data> <SText id="p1"> Thanks in advance! PHP: <form method="post"> <input name="ken1" id="ken1" type="text"> <br> <input type="submit" name="submit" value="submit"> </form> <?php if(isset($_POST['submit'])) { $data=simplexml_load_file('display.xml'); $data->data->SText->ken1->text=$_POST['ken1']; $handle=fopen("display.xml","wb"); fwrite($handle,$data->asXML()); fclose($handle); } $data=simplexml_load_file('display.xml'); ?> XML file: <kendisplay> <meta> <request>setData</request> <version>1</version> </meta> <data> <SText id="p1"> <text>test</text> </SText> <SText id="p2"> <text>test</text> </SText> <SText id="ken1"> <text>test</text> </SText> <SText id="wait"> <text>test</text> </SText> </data> </kedisplay> Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/ Share on other sites More sharing options...
requinix Posted February 10, 2022 Share Posted February 10, 2022 Use ->xpath to go directly to the right <text>. /kendisplay/data/SText[@id="ken1"]/text Keep in mind that XPath queries are like SQL and HTML: if you put user-provided data into them (like if "ken1" came from the form instead of your own fingers) then it needs to be escaped first, such as with sprintf('/kendisplay/data/SText[@id="%s"]/text', htmlspecialchars($id)) Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594051 Share on other sites More sharing options...
RRO Posted February 10, 2022 Author Share Posted February 10, 2022 Thanks for your reply. However, I'm not getting this implemented properly yet. How can I best include this in my code? Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594053 Share on other sites More sharing options...
requinix Posted February 10, 2022 Share Posted February 10, 2022 What code have you tried so far and what happened when you used it? Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594054 Share on other sites More sharing options...
RRO Posted February 10, 2022 Author Share Posted February 10, 2022 I tried your suggestion but got a syntax error: Quote Parse error: syntax error, unexpected '=', expecting ']' in And have searched for php xpath xml but don't know what to use. Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594055 Share on other sites More sharing options...
dodgeitorelse3 Posted February 10, 2022 Share Posted February 10, 2022 Always show your current code. Much easier to find syntax errors as well as any other mistakes possibly entered as code. Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594056 Share on other sites More sharing options...
Barand Posted February 10, 2022 Share Posted February 10, 2022 14 minutes ago, RRO said: And have searched for php xpath xml but don't know what to use. https://www.php.net/manual/en/simplexmlelement.xpath.php Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594057 Share on other sites More sharing options...
RRO Posted February 11, 2022 Author Share Posted February 11, 2022 11 hours ago, dodgeitorelse3 said: Always show your current code. Much easier to find syntax errors as well as any other mistakes possibly entered as code. 15 hours ago, RRO said: PHP: <form method="post"> <input name="ken1" id="ken1" type="text"> <br> <input type="submit" name="submit" value="submit"> </form> <?php if(isset($_POST['submit'])) { $data=simplexml_load_file('display.xml'); $data->data->SText->ken1->text=$_POST['ken1']; $handle=fopen("display.xml","wb"); fwrite($handle,$data->asXML()); fclose($handle); } $data=simplexml_load_file('display.xml'); ?> XML file: <kendisplay> <meta> <request>setData</request> <version>1</version> </meta> <data> <SText id="p1"> <text>test</text> </SText> <SText id="p2"> <text>test</text> </SText> <SText id="ken1"> <text>test</text> </SText> <SText id="wait"> <text>test</text> </SText> </data> </kedisplay> Also tried: $data/kendisplay/data/SText[@id="ken1"]/text=$_POST['ken1']; Here I get a syntax error. Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594072 Share on other sites More sharing options...
Barand Posted February 11, 2022 Share Posted February 11, 2022 Conspicuously absent is any attempt to use xpath as you were advised. Obviously spoonfeeding is required. Here's my version (slightly different from Requinix's, but not much) $xmlstr = <<<XML <kendisplay> <meta> <request>setData</request> <version>1</version> </meta> <data> <SText id="p1"> <text>test</text> </SText> <SText id="p2"> <text>test</text> </SText> <SText id="ken1"> <text>test</text> </SText> <SText id="wait"> <text>test</text> </SText> </data> </kendisplay> XML; $target_id = 'ken1'; $newtext = 'Updated'; $xml = simplexml_load_string($xmlstr); $path = sprintf("//SText[@id='%s']", htmlspecialchars($target_id) ); $ken = $xml->xpath($path); // find the SText $ken[0]->text = htmlspecialchars($newtext); // update its text $xml->asXML('test.xml'); // output updated xml file test.xml output ... <?xml version="1.0"?> <kendisplay> <meta> <request>setData</request> <version>1</version> </meta> <data> <SText id="p1"> <text>test</text> </SText> <SText id="p2"> <text>test</text> </SText> <SText id="ken1"> <text>Updated</text> </SText> <SText id="wait"> <text>test</text> </SText> </data> </kendisplay> Quote Link to comment https://forums.phpfreaks.com/topic/314546-how-to-select-id-by-changing-xml-file-with-php/#findComment-1594074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.