1

我需要发送一串html就像 <total> <tag>content</tag> <tag2 ref="333"> <code>somecode</code> <code>morecode</code> </tag2> <tag3> more code </tag3> </total>

这将进入一个数组,如:

$arra[] = "<tag>content</tag>";
$arra[] = "<tag2 ref="333">";
$arra[] = "<code ... etc

但我不知道如何将这些数据转换为数组。

有小费吗 ?

4

5 回答 5

2

简单地:

$lines = explode('> ', $xml);
foreach($lines as $line) {
  $arra[] = $line.'> ';
}

然而,这是假设给出的示例是您的 XML 的精确表示(即您在标记块中使用 > 和 <),并且没有考虑 CDATA 块或 XML 注释。http://www.w3.org/TR/REC-xml/#syntax

否则我会查看 PHP.net 页面上的评论:http: //us.php.net/manual/en/function.xml-parse.php

于 2009-10-30T16:23:30.687 回答
2

你到底想做什么?如果您对您要解决的问题进行更广泛的了解,我们可能会提供更好的解决方案。

于 2009-10-30T21:10:52.637 回答
2

所以你想转换这个树数据结构:

<total> 
  <tag>content</tag> 
  <tag2 ref="333"> 
    <code>somecode</code> 
    <code>morecode</code> 
  </tag2> 
  <tag3> more code </tag3> 
</total>

进入某种平面数组:

Array
(
    [0] => "<tag>content</tag>"
    [1] => "<tag2 ref="333"></tag2>"
    [2] => "<code>somecode</code>"
    [3] => "<code>morecode</code> 
    [4] => "<tag3> more code </tag3> "
)

会很棘手。这是一个经典的 CS 问题,没有很多好的答案。树结构提供了平面数组或列表所没有的条目之间的关系信息。任何将树扁平化为列表的尝试都会失去该引用上下文。

您可以分解字符串,然后遍历它以跟踪父元素或忽略它们(参见 tag2)。如果我必须对 xml 做一些事情,我会将它放在 SimpleXMLElement 中,这将产生如下内容:

SimpleXMLElement Object
(
    [tag] => content
    [tag2] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [ref] => 333
                )
            [code] => Array
                (
                    [0] => somecode
                    [1] => morecode
                )
        )
    [tag3] =>  more code 
)

有了这个,我可以用 foreach 遍历它并找出标签及其内容。我可以测试看看内容是字符串还是子元素,如果是的话,遍历它们。递归函数可以很短地解决这个问题。最大的问题是如何在数据变平后表示数据。

如果将它展平为我之前提供的数组示例,则父标签和子标签之间会失去任何隐含关系。如果这不是问题,那就太好了。编写递归函数,你就完成了。这是一些伪代码:

function walking($content)
  $out is the array chunk that is returned
  foreach $content as $tag->$data
    if $value is an SimpleXMLElement
      collapse $data[@attributes] into a string $attributes
      append <$tag $attributes></$tag> to the end of $out
      you may need to remove @attributes before recursing.
      recurse into  walking($data) and append the returned array to the end of $out
    if $value is an Array
      append <$tag></$tag> to the end of $out
      recurse into  walking($data) and append the returned array to the end of $out
    if $value is a string
      append <$tag>$value</$tag> to the end of $out
  after looping through $content return $out.

但是,如果您需要以某种方式保持这些关系完好无损,您就会遇到一些问题,并且需要为此设计某种方案

于 2009-10-30T21:34:14.083 回答
0

如果您从文件中获取数据,则 file() 函数将为您提供每行一行的数组。可能是最简单的方法!

于 2009-10-30T21:37:10.397 回答
0
$xml = file_get_contents("../../xml/LL1234.xml");

$x = simplexml_load_string($xml);

function viewElements($x){
    $Arr = $GLOBALS['Arr'];
    if (count($x->attributes()) > 0 ){
        $attr='';
        foreach ($x->attributes() as $k => $v ){
                $attr .= " $k='".$v."'";
        }
    }

    $Arr[] = "<".$x->getName()." $attr>\n";
    if (count($x->children()) > 0 ){
        foreach ($x->children() as $k ){
            $GLOBALS['Arr'] = $Arr;
            viewElements($k);
            $Arr = $GLOBALS['Arr'];
        }
    }else{
        $Arr[] = $x[0];
    }

    $Arr[] = "</".$x->getName().">";
    $GLOBALS['Arr'] = $Arr;
}

foreach ($x->children() as $k ){
    viewElements($k);
}

foreach ($GLOBALS['Arr'] as $k ){
    print $k."\n";
}
?>

抱歉,感谢 tvanover 给我带来的麻烦,我了解了一种更好的方法,解决方案如下。

于 2009-11-02T15:58:30.933 回答