Asked  8 Months ago    Answers:  5   Viewed   29 times

I want to set text of some node found by xpath()

<?php

$args = new SimpleXmlElement(
<<<XML
<a>
  <b>
    <c>text</c>
    <c>stuff</c>
  </b>
  <d>
    <c>code</c>
  </d>
</a>
XML
);

// I want to set text of some node found by xpath 
// Let's take (//c) for example

// convoluted and I can't be sure I'm setting right node
$firstC = reset($args->xpath("//c[1]/parent::*")); 
$firstC->c[0] = "test 1";

// like here: Found node is not actually third in its parent.
$firstC = reset($args->xpath("(//c)[3]/parent::*")); 
$firstC->c[2] = "test 2";

// following won't work for obvious reasons, 
// some setText() method would be perfect but I can't find nothing similar, 
$firstC = reset($args->xpath("//c[1]"));
$firstC = "test"; 

// maybe there's some hack for it?
$firstC = reset($args->xpath("//c[1]"));
$firstC->{"."} = "test"; // nope, just adds child named .
$firstC->{""} = "test"; // still not right, 'Cannot write or create unnamed element'
$firstC["."] = "test"; // still no luck, adds attribute named .
$firstC[""] = "test"; // still no luck, 'Cannot write or create unnamed attribute'
$firstC->addChild('','test'); // grr, 'SimpleXMLElement::addChild(): Element name is required'
$firstC->addChild('.','test'); // just adds another child with name .

echo $args->asXML();

// it outputs:
// 
// PHP Warning:  main(): Cannot add element c number 2 when only 1 such elements exist 
// PHP Warning:  main(): Cannot write or create unnamed element 
// PHP Warning:  main(): Cannot write or create unnamed attribute 
// PHP Warning:  SimpleXMLElement::addChild(): Element name is required 
// <?xml version="1.0"? >
// <a>
//  <b>
//   <c .="test">test 1<.>test</.><.>test</.></c>
//   <c>stuff</c>
//  </b>
//  <d>
//   <c>code</c>
//  <c>test 2</c></d>
// </a>

 Answers

51

You can do with a SimpleXMLElement self-reference:

$firstC->{0} = "Victory!!"; // hackity, hack, hack!
//  -or-
$firstC[0]   = "Victory!!";

found after looking at

var_dump((array) reset($xml->xpath("(//c)[3]")))

This also works with unset operations as outlined in an answer to:

  • Remove a child with a specific attribute, in SimpleXML for PHP
Wednesday, March 31, 2021
 
Saxophlutist
answered 8 Months ago
31

this might help

$obj = new SimpleXMLElement($xml);
$rtn = array();
$cnt = 0;
foreach($obj->xpath('///OSes/*/*') as $rec)
{
  foreach ($rec as $rec_obj)
  {
    if (!isset($rtn[$cnt]))
    {
      $rtn[$cnt] = array();
    }

    foreach ($rec_obj as $name=>$val)
    {
      $rtn[$cnt][(string)$name] = (string)$val;
    }
    ++$cnt;
  }
}
Saturday, May 29, 2021
 
dimitarvp
answered 5 Months ago
17

You were probably not "cycling" the countries and cities:

<?php
    $xml_file = '/path';
    $xml = simplexml_load_file($xml_file);

    foreach($xml->Continent as $continent) {
        echo "<div class='continent'>".$continent['Name']."<span class='status'>".$continent['Status']."</span>";
        foreach($continent->Country as $country) {
            echo "<div class='country'>".$country['Name']."<span class='status'>".$country['Status']."</span>";
            foreach($country->City as $city) {
                echo "<div class='city'>".$city['Name']."<span class='status'>".$city['Status']."</span></div>";
            }
            echo "</div>"; // close Country div
        }
        echo "</div>"; // close Continent div
    }
?>
Saturday, May 29, 2021
 
avon_verma
answered 5 Months ago
14
$("#mydropdownlist").val("thevalue");

just make sure the value in the options tags matches the value in the val method.

Tuesday, June 8, 2021
 
nhunston
answered 5 Months ago
15

The problem is solved. It was because of the way I call javascript in java code.

code like

mWebView.loadUrl("javascript: [your javascript code]");

sometimes work, but it doesn't work in all the case. At least it doesn't work for the following case when I tried to set the textbox in a webview:

mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').value= 'test'");

but it should work if you call the javascript as a function:

mWebView.loadUrl("javascript: (function() {document.getElementById('xxxxxxxx').value= 'test';}) ();" );

That fixed my problem. Hope it is helpful.

Wednesday, September 22, 2021
 
jvf
answered 1 Month ago
jvf
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share