2012-06-01 47 views
0

我得到一个simplexml变量存储一个xml文件,我需要做的是将其转换为字符串,然后将其保存在SQL数据库文件中,我试过:XML以字符串保存在SQL(PHP)

echo $xml->asXML(); 

但它只显示我的标签的内容,我需要得到所有的结构来保存它。

有没有办法从PHP中的XML变量中获取完整字符串?

在此先感谢

+0

请提供其余代码,因为提供的单行代码会将整个XML作为字符串进行回显。 –

+0

你的'$ xml'出错了,因为'$ xml-> asXML();'*会*做你所问的。 – Madbreaks

+0

你什么时候说“string”你是指JSON?或只是采取.xml,并只是一个字符串?在这种情况下,为什么不file_get_contents($ xml_file); ?? – 2012-06-01 17:32:23

回答

1

好有人在这里说,简单地使用

$test = $xml_variable->asXML(); 

它会将整个xml存储在$ test变量中,唯一的问题是当您回显它时,它只会显示标签值,而不是标签本身。这就是为什么我没有工作。

无论如何,我更新了我的sql表中的xml字段,它填充了格式良好的xml字符串。

0

您可以使用PHP的XML解析功能。

php.net/manual/en/function.xml-parse.php#87920

<?php 
function xml2array($url, $get_attributes = 1, $priority = 'tag') 
{ 
    $contents = ""; 
    if (!function_exists('xml_parser_create')) 
    { 
     return array(); 
    } 
    $parser = xml_parser_create(''); 
    if (!($fp = @ fopen($url, 'rb'))) 
    { 
     return array(); 
    } 
    while (!feof($fp)) 
    { 
     $contents .= fread($fp, 8192); 
    } 
    fclose($fp); 
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    xml_parse_into_struct($parser, trim($contents), $xml_values); 
    xml_parser_free($parser); 
    if (!$xml_values) 
     return; //Hmm... 
    $xml_array = array(); 
    $parents = array(); 
    $opened_tags = array(); 
    $arr = array(); 
    $current = & $xml_array; 
    $repeated_tag_index = array(); 
    foreach ($xml_values as $data) 
    { 
     unset ($attributes, $value); 
     extract($data); 
     $result = array(); 
     $attributes_data = array(); 
     if (isset ($value)) 
     { 
      if ($priority == 'tag') 
       $result = $value; 
      else 
       $result['value'] = $value; 
     } 
     if (isset ($attributes) and $get_attributes) 
     { 
      foreach ($attributes as $attr => $val) 
      { 
       if ($priority == 'tag') 
        $attributes_data[$attr] = $val; 
       else 
        $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' 
      } 
     } 
     if ($type == "open") 
     { 
      $parent[$level -1] = & $current; 
      if (!is_array($current) or (!in_array($tag, array_keys($current)))) 
      { 
       $current[$tag] = $result; 
       if ($attributes_data) 
        $current[$tag . '_attr'] = $attributes_data; 
       $repeated_tag_index[$tag . '_' . $level] = 1; 
       $current = & $current[$tag]; 
      } 
      else 
      { 
       if (isset ($current[$tag][0])) 
       { 
        $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
        $repeated_tag_index[$tag . '_' . $level]++; 
       } 
       else 
       { 
        $current[$tag] = array (
         $current[$tag], 
         $result 
        ); 
        $repeated_tag_index[$tag . '_' . $level] = 2; 
        if (isset ($current[$tag . '_attr'])) 
        { 
         $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
         unset ($current[$tag . '_attr']); 
        } 
       } 
       $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; 
       $current = & $current[$tag][$last_item_index]; 
      } 
     } 
     elseif ($type == "complete") 
     { 
      if (!isset ($current[$tag])) 
      { 
       $current[$tag] = $result; 
       $repeated_tag_index[$tag . '_' . $level] = 1; 
       if ($priority == 'tag' and $attributes_data) 
        $current[$tag . '_attr'] = $attributes_data; 
      } 
      else 
      { 
       if (isset ($current[$tag][0]) and is_array($current[$tag])) 
       { 
        $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
        if ($priority == 'tag' and $get_attributes and $attributes_data) 
        { 
         $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
        } 
        $repeated_tag_index[$tag . '_' . $level]++; 
       } 
       else 
       { 
        $current[$tag] = array (
         $current[$tag], 
         $result 
        ); 
        $repeated_tag_index[$tag . '_' . $level] = 1; 
        if ($priority == 'tag' and $get_attributes) 
        { 
         if (isset ($current[$tag . '_attr'])) 
         { 
          $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
          unset ($current[$tag . '_attr']); 
         } 
         if ($attributes_data) 
         { 
          $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
         } 
        } 
        $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken 
       } 
      } 
     } 
     elseif ($type == 'close') 
     { 
      $current = & $parent[$level -1]; 
     } 
    } 
    return ($xml_array); 
} 
?> 
+0

这返回我一个PHP数组,安吗?我需要的是与所有的标签等,将其存储在我的数据库作为一个字符串 –

0

也许尝试的DomDocument:

$xml = new DomDocument(); 
$xml->loadXML($xml_str); 
echo $xml->saveXML(); 

供参考:http://us.php.net/manual/en/domdocument.loadxml.php

+0

我得到存储在PHP变量$ my_xml 尝试这样我的XML字符串,但它不工作: $ XML =新的DomDocument() ; $ xml-> loadXML($ xml_cliente); echo $ xml-> saveXML(); –

+0

对不起,我改变了我的答案。 loadXML()接受一个xml字符串,而不是文件名。 – gregghz