【问题标题】:Retrieve a value stored within an XML using PHP使用 PHP 检索存储在 XML 中的值
【发布时间】:2014-12-15 07:33:34
【问题描述】:
我想导航到特定的 url,不想每次都硬编码该值。有没有办法调用“SELENIUM_BASEURL”来代替。
$this->url('http://www.google.com');
phpunit.xml...
<php>
<!-- define some constants to use in our test classes -->
<const name="SELENIUM_HOST" value="127.0.0.1"/>
<const name="SELENIUM_BASEURL" value="http://www.google.com"/>
</php>
<testsuites>
<testsuite name="SeleniumSetup">
<directory>tests/integration/</directory>
</testsuite>
</testsuites>
【问题讨论】:
标签:
php
selenium
selenium-webdriver
phpunit
【解决方案1】:
是的,在这种情况下你可以使用SimpleXML:
$xml = simplexml_load_file('phpunit.xml');
$element = $xml->xpath('//php/const[@name="SELENIUM_BASEURL"]');
$selenium_baseurl = $element[0]->attributes()->value;
echo $selenium_baseurl; // http://www.google.com
或者使用DOMDocument:
$dom = new DOMDocument();
$dom->load('phpunit.xml');
$xpath = new DOMXpath($dom);
$selenium_baseurl = $xpath->evaluate('string(//php/const[@name="SELENIUM_BASEURL"]/@value)');
echo $selenium_baseurl; // http://www.google.com
然后:
$this->url($seleniium_baseurl);
【解决方案2】:
你已经在你的 XML 中定义了常量,所以你可以用通常的方式访问它们:
$this->url(SELENIUM_BASEURL);