Jump to content

How to select ID by changing xml file with PHP


RRO

Recommended Posts

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>

 

Link to comment
Share on other sites

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))

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.