数组解析的问题,已经困扰我一天了 没搞XML不过理论上应该是可行的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的xml文件解析成了两个结构数组,一个是键,一个是键值,你只要能得到所有的标签及结果,那你需要什么样子的数组,你都可以拼凑起来的。 1、既然你能解析出二维数组,那么就同样可以解析出巢状数组。况且解析出巢状数组要比二维数组简单2、你示例中的数组与你示例中的xml文档并不相符 to:唠叨2."你示例中的数组与你示例中的xml文档并不相符"---- 这是我自己在做测试的时候用的,没看仔细就帖上来了,不好意思以下是我现在写的php文件(调试中的,问题我已经标识出来了)<?phpClass Files { function fileExists($filename) { return (is_file($filename)) ? true : false; } function loadFile($file) { return (Files::fileExists($file)) ? file_get_contents($file) : ""; }}Class XML { var $tag; var $type; var $level; var $value; function XML() { } function setTag($tag) { $this->tag = $tag; } function setType($type) { $this->type = $type; } function setLevel($level) { $this->type = $level; } function setValue($value) { $this->value = $value; }}Class XMLParser { var $parser; var $rs = array(); var $_rs = array(); var $index = array(); var $debug = false; function XMLParser() { $this->parser = xml_parser_create(); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); register_shutdown_function(array(&$this, "_shutdown")); } function parse($file) { if (!$data = Files::loadFile($file)) { $this->error[] = "no data"; return false; } xml_parse_into_struct($this->parser, $data, $this->rs, $this->index); print_r($this->rs); print_r($this->index); } function createNewArray() { }/** * 这个函数写的有问题 * 是逻辑上有问题 * 我现在晕了,不知道该怎么写了*/ function getResult() { $i = 0; foreach ($this->rs as $key => $val) { if ($val['type'] == 'open') { $this->_rs[$i] = $this->_result($key); $i++; } else if ($val['type'] == 'close') { continue; } else { if (is_object($tmp)) { $tmp->setTag($val['tag']); $tmp->setValue($val['value']); } } } print_r($this->_rs); } function _result($key) { if (!is_array($this->rs[$key])) return array(); $obj = new XML(); $obj->setTag($this->rs[$key]['tag']); $obj->setValue($this->rs[$key]['value']); return $obj; }/* 到这里结束 */ function debug() { $this->debug = true; } function _shutdown() { if ($this->debug && count($this->error) > 0) { echo "<pre>"; foreach ($this->error as $key => $val) { echo "<B><font color=red>"; echo $key+1 .": ". $val; echo "</font></B><br />\r\n"; } echo "</pre>"; } }}echo "<pre>";$xml_parser = new XMLParser();$xml_parser->debug = true;$xml_parser->parse("1.xml");$xml_parser->getResult();//print_r($xml_parser->getRs());?>另外附上XML文件和XML解析出来的数组1.xml<?xml version="1.0"?><moldb> <molecule> <name>Alanine</name> <symbol>ala</symbol> <code>A</code> <type>hydrophobic</type> </molecule> <molecule> <name>Lysine</name> <symbol>lys</symbol> <code>K</code> <type>charged</type> </molecule></moldb>解析出来的数组:结果数组Array( [0] => Array ( [tag] => moldb [type] => open [level] => 1 ) [1] => Array ( [tag] => molecule [type] => open [level] => 2 ) [2] => Array ( [tag] => name [type] => complete [level] => 3 [value] => Alanine ) [3] => Array ( [tag] => symbol [type] => complete [level] => 3 [value] => ala ) [4] => Array ( [tag] => code [type] => complete [level] => 3 [value] => A ) [5] => Array ( [tag] => type [type] => complete [level] => 3 [value] => hydrophobic ) [6] => Array ( [tag] => molecule [type] => close [level] => 2 ) [7] => Array ( [tag] => molecule [type] => open [level] => 2 ) [8] => Array ( [tag] => name [type] => complete [level] => 3 [value] => Lysine ) [9] => Array ( [tag] => symbol [type] => complete [level] => 3 [value] => lys ) [10] => Array ( [tag] => code [type] => complete [level] => 3 [value] => K ) [11] => Array ( [tag] => type [type] => complete [level] => 3 [value] => charged ) [12] => Array ( [tag] => molecule [type] => close [level] => 2 ) [13] => Array ( [tag] => moldb [type] => close [level] => 1 ))索引数组:Array( [moldb] => Array ( [0] => 0 [1] => 13 ) [molecule] => Array ( [0] => 1 [1] => 6 [2] => 7 [3] => 12 ) [name] => Array ( [0] => 2 [1] => 8 ) [symbol] => Array ( [0] => 3 [1] => 9 ) [code] => Array ( [0] => 4 [1] => 10 ) [type] => Array ( [0] => 5 [1] => 11 ))我想得到的结果:一个类似这样的对象数组XML Object( "tag" => "moldb", "type" => "open", "level" => "1", "value" => Array ( "0" => "XML Object ( "tag" => "molecule", "type" => "open", "level" => "2", "value" => Array ( "0" => Array ( "name" => "Alanine", "symbol"=> "ala", "code" => "A", "type" => "hydrophobic", ), ), ), "1" => "XML Object ( "tag" => "molecule", "type" => "open", "level" => "2", "value" => Array ( "0" => Array ( "name" => "Lysine", "symbol"=> "lys", "code" => "K", "type" => "charged", ), ), ), ),)有点长,希望各位能耐心一点,帮忙解决这个问题:) xml为什么不用这种形式? <molecule> <name>Lysine</> <symbol>lys</> <code>K</> <type>charged</> </> ???这样语法有问题的吧我这个xml是随手从手册上拉下来的~ 感觉你的逻辑有点乱,不知道你具体想要干什么,XML解析出来就是一棵树 我PHP和asp都有一定基础不知道专研那一种好。。 PHP不能用面向对象方式连接数据库是什么原因? 这个sql怎么在mysql3.23下不行? 急求解```!!进!!!! 有关smarty变量使用的问题 局域网内访问的问题!! php程序在linux下,oracle在win2000下,这样可以连oracle吗? 请问textarea表单提交的段落怎样显示分段,急! POST && GET 的区别??!!waiting...关注ing... PHP本地访问的问题 用代理上网得怎么知道它得IP,而不是代理得IP。 求助]图片与标题同步的问题,请教大家!
2、你示例中的数组与你示例中的xml文档并不相符
2."你示例中的数组与你示例中的xml文档并不相符"
---- 这是我自己在做测试的时候用的,没看仔细就帖上来了,不好意思以下是我现在写的php文件(调试中的,问题我已经标识出来了)<?php
Class Files { function fileExists($filename) { return (is_file($filename)) ? true : false;
} function loadFile($file) { return (Files::fileExists($file)) ? file_get_contents($file) : "";
}}Class XML { var $tag;
var $type;
var $level;
var $value; function XML() {
} function setTag($tag) {
$this->tag = $tag;
} function setType($type) {
$this->type = $type;
} function setLevel($level) {
$this->type = $level;
} function setValue($value) {
$this->value = $value;
}
}Class XMLParser { var $parser; var $rs = array();
var $_rs = array();
var $index = array(); var $debug = false; function XMLParser() { $this->parser = xml_parser_create(); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); register_shutdown_function(array(&$this, "_shutdown"));
} function parse($file) { if (!$data = Files::loadFile($file)) { $this->error[] = "no data";
return false;
} xml_parse_into_struct($this->parser, $data, $this->rs, $this->index);
print_r($this->rs);
print_r($this->index); } function createNewArray() {
}
/**
* 这个函数写的有问题
* 是逻辑上有问题
* 我现在晕了,不知道该怎么写了
*/
function getResult() { $i = 0;
foreach ($this->rs as $key => $val) {
if ($val['type'] == 'open') {
$this->_rs[$i] = $this->_result($key);
$i++;
} else if ($val['type'] == 'close') {
continue;
} else {
if (is_object($tmp)) {
$tmp->setTag($val['tag']);
$tmp->setValue($val['value']);
}
} }
print_r($this->_rs);
} function _result($key) { if (!is_array($this->rs[$key])) return array(); $obj = new XML();
$obj->setTag($this->rs[$key]['tag']);
$obj->setValue($this->rs[$key]['value']); return $obj;
}
/* 到这里结束 */
function debug() { $this->debug = true;
} function _shutdown() {
if ($this->debug && count($this->error) > 0) {
echo "<pre>";
foreach ($this->error as $key => $val) {
echo "<B><font color=red>";
echo $key+1 .": ". $val;
echo "</font></B><br />\r\n";
}
echo "</pre>";
}
}
}echo "<pre>";
$xml_parser = new XMLParser();
$xml_parser->debug = true;
$xml_parser->parse("1.xml");
$xml_parser->getResult();//print_r($xml_parser->getRs());?>另外附上XML文件和XML解析出来的数组
1.xml
<?xml version="1.0"?>
<moldb> <molecule>
<name>Alanine</name>
<symbol>ala</symbol>
<code>A</code>
<type>hydrophobic</type>
</molecule> <molecule>
<name>Lysine</name>
<symbol>lys</symbol>
<code>K</code>
<type>charged</type>
</molecule></moldb>
解析出来的数组:结果数组
Array
(
[0] => Array
(
[tag] => moldb
[type] => open
[level] => 1
) [1] => Array
(
[tag] => molecule
[type] => open
[level] => 2
) [2] => Array
(
[tag] => name
[type] => complete
[level] => 3
[value] => Alanine
) [3] => Array
(
[tag] => symbol
[type] => complete
[level] => 3
[value] => ala
) [4] => Array
(
[tag] => code
[type] => complete
[level] => 3
[value] => A
) [5] => Array
(
[tag] => type
[type] => complete
[level] => 3
[value] => hydrophobic
) [6] => Array
(
[tag] => molecule
[type] => close
[level] => 2
) [7] => Array
(
[tag] => molecule
[type] => open
[level] => 2
) [8] => Array
(
[tag] => name
[type] => complete
[level] => 3
[value] => Lysine
) [9] => Array
(
[tag] => symbol
[type] => complete
[level] => 3
[value] => lys
) [10] => Array
(
[tag] => code
[type] => complete
[level] => 3
[value] => K
) [11] => Array
(
[tag] => type
[type] => complete
[level] => 3
[value] => charged
) [12] => Array
(
[tag] => molecule
[type] => close
[level] => 2
) [13] => Array
(
[tag] => moldb
[type] => close
[level] => 1
))索引数组:
Array
(
[moldb] => Array
(
[0] => 0
[1] => 13
) [molecule] => Array
(
[0] => 1
[1] => 6
[2] => 7
[3] => 12
) [name] => Array
(
[0] => 2
[1] => 8
) [symbol] => Array
(
[0] => 3
[1] => 9
) [code] => Array
(
[0] => 4
[1] => 10
) [type] => Array
(
[0] => 5
[1] => 11
))我想得到的结果:一个类似这样的对象数组
XML Object
(
"tag" => "moldb",
"type" => "open",
"level" => "1",
"value" => Array
(
"0" => "XML Object
(
"tag" => "molecule",
"type" => "open",
"level" => "2",
"value" => Array
(
"0" => Array
(
"name" => "Alanine",
"symbol"=> "ala",
"code" => "A",
"type" => "hydrophobic",
),
),
),
"1" => "XML Object
(
"tag" => "molecule",
"type" => "open",
"level" => "2",
"value" => Array
(
"0" => Array
(
"name" => "Lysine",
"symbol"=> "lys",
"code" => "K",
"type" => "charged",
),
),
),
),
)有点长,希望各位能耐心一点,帮忙解决这个问题:)
<molecule>
<name>Lysine</>
<symbol>lys</>
<code>K</>
<type>charged</>
</>
这样语法有问题的吧我这个xml是随手从手册上拉下来的~