我正在尝试创建一个PHP脚本来从string.xml android获取值。从文件中提取XML文件然后解析XML是一种方式。
这是string.xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources>
<string name="btn_search">Search</string>
<string name="btn_around">Around</string>
<string name="btn_load">Load more</string>
<string name="btn_load_more">Load More</string>
<string name="loading">Loading</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_pharmacy">Pharmacies</string>
<string name="title_activity_around">AroundActivity</string>
<string name="gps_network_enabled">GPS or Network is not enabled</string>
<string name="action_settings">Settings</string>
<string-array name="fi">
<item>A</item>
<item>Almak</item>
<item>Anlamak</item>
<item>Anlasmak</item>
<item>Anlatmak</item>
<item>Ayrilmak</item>
</string-array> </resources>
这是我的简单剧本:
$xml = simplexml_load_file(XML_UPLOAD_PATH . $file_name);
var_dump($xml);
as rezult是:
object(SimpleXMLElement)[1]
public 'string' =>
array (size=2)
0 => string 'Search' (length=6)
1 => string 'Around' (length=6)
public 'string-array' =>
object(SimpleXMLElement)[2]
public '@attributes' =>
array (size=1)
'name' => string 'fi' (length=2)
public 'item' =>
array (size=2)
0 => string 'A' (length=1)
1 => string 'Almak' (length=5)
第一个问题我没有从字符串节点获取属性名称。问题是什么是组织阵列输出的最佳实践。
答案 0 :(得分:0)
如果要访问属性,请参阅XML解析示例(由PHP手册提供) -
$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
如果要将示例转换为数组,可以执行以下操作 -
$xml = simplexml_load_string($string, XML_UPLOAD_PATH . $file_name, LIBXML_NOCDATA);
$array = json_decode(json_encode((array)$xml), TRUE);
答案 1 :(得分:0)
我也试图找到一种通过PHP读取strings.xml文件的方法。当我使用simplexml_load_file时,由于某种原因,所有属性都消失了?
我发现<resources>
标签导致了问题,如果我在<string>
标签上使用了simplexml_load_string,那就没关系了。
我使这个函数打破了每一行,所以我可以在每个<string>
标签上调用simplexml_load_file:
parse_xml('
<resources>
<string name="app_name" translatable="false">My App</string>
<string name="string_name_example">Some text</string>
</resources>
');
function parse_xml($xml_file_contents = '') {
// remove whitespace and return carriages in between tags
$xml_file_contents = preg_replace("#\>((\n|\r)\s+)\<#","><",$xml_file_contents);
// remove any left over return carriages and tabs
$xml_file_contents = preg_replace("#\n|\r|\t#","",$xml_file_contents);
// remove comments
$xml_file_contents = preg_replace("#\<\!\-(.*?)\-\>#","",$xml_file_contents);
// explode the data
$xml_explode = explode('><',$xml_file_contents);
$xml_data = [];
foreach ($xml_explode as &$line) {
$line = trim($line);
$line = preg_replace('#^\<|\>$#','',$line);
$line = sprintf("<%s>",$line);
if (preg_match('#\>.*?\<#',$line,$matches)) {
// valid
$xml_data[] = json_decode(json_encode(simplexml_load_string($line)),true);
}
}
return $xml_data;
}
这似乎对我有用并产生以下输出
Array
(
[0] => Array
(
[@attributes] => Array
(
[name] => app_name
[translatable] => false
)
[0] => My App
)
[1] => Array
(
[@attributes] => Array
(
[name] => string_name_example
)
[0] => Some text
)
)
根据xml数据的大小,爆炸功能可能会提高内存使用率,因此请注意!