用PHP中的XML值创建XML?

时间:2015-02-27 06:18:22

标签: php xml excel

我正在从字符串创建XML(此字符串是另一个XML的值)。

我尝试simplexml_load_string来创建。

我的场景:

我的用户界面在php中,我通过PHPExcel将Excel工作表数据发送到我项目的服务部分。它返回:

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
   <root>
     <searcheddata>
         {Account0={AccountName=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0},  City1={City=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0},  State2={State=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, Zip=0.0, AlternateEmail=0.0},  Country3={Country=1, AddressLine2=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}}
        </searcheddata>
    </root>

现在我尝试使用<searcheddata>的值创建XML。

Atlast我需要一个XML:

<xml>
     <root>
        <Account0>
            <AccountName>1</AccountName>
            <AddressLine2>0.0</AddressLine2>
            <Country>0.0</Country>
            <CellPhone>0.0</CellPhone>
            <City>0.0</City>
            <WorkPhone>0.0</WorkPhone>
            <Other>0.0</Other>
            <HomePhone>0.0</HomePhone>
            <AddressLine1>0.0</AddressLine1>
            <State>0.0</State>
            <Zip>0.0</Zip>
            <AlternateEmail>0.0</AlternateEmail>
        </Account0>
        <City1>
            <City>1</City>
            <AddressLine2>0.0</AddressLine2>
            <Country>0.0</Country>
            <CellPhone>0.0</CellPhone>
            <WorkPhone>0.0</WorkPhone>
            <Other>0.0</Other>
            <HomePhone>0.0</HomePhone>
            <AddressLine1>0.0</AddressLine1>
            <State>0.0</State>
            <Zip>0.0</Zip>
            <AlternateEmail>0.0</AlternateEmail>
        </City1>
     </root>
 </xml>

我尝试了什么:

<?php

$str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><root><searcheddata>{Account0={AccountName=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0},  City1={City=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0},  State2={State=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, Zip=0.0, AlternateEmail=0.0},  Country3={Country=1, AddressLine2=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}}</searcheddata></root>";

$a = array();
$name = array();
$values = array();
$mainArray = array();
$xml = simplexml_load_string($str);
$s3 = $xml->searcheddata;
$pieces = (explode("},", $s3));
$pieces[0] = substr($pieces[0], 1);
$length = sizeof($pieces);
$pieces[$length - 1] = substr($pieces[$length - 1], 0, -2);
foreach ($pieces as $value) {
    $morePiece = (explode("={", $value));
    for ($i = 0; $i < sizeof($morePiece); $i = $i + 2) {
        $lastPiece = explode(", ", $morePiece[$i]);
        for ($j = 0; $j < sizeof($lastPiece); $j++) {
            array_push($a, $lastPiece[0]);
        }
    }
    for ($i = 1; $i < sizeof($morePiece); $i = $i + 2) {
        $lastPiece2 = explode(", ", $morePiece[$i]);
        for ($j = 0; $j < sizeof($lastPiece2); $j++) {
            $finalPiece2 = explode("=", $lastPiece2[$j]);
            for ($k = 0; $k < sizeof($finalPiece2); $k++) {
                if ($k % 2 == 0) {
                    array_push($name, $finalPiece2[$k]);
                } else {
                    array_push($values, $finalPiece2[$k]);
                }
            }
        }
    }
}
echo sizeof($pieces)."<br>";
print_r($a);
echo '<br><br>';
print_r($name);
echo '<br><br>';
print_r($values);
echo '<br><br>';
?>

我试图将所有密钥和值放在相应的数组中,然后使用simplexml-load-string创建XML。

欢迎提出建议。

1 个答案:

答案 0 :(得分:2)

努力避免编写完整的解析器,以下结合了一些正则表达式和来自不同格式(字符串,JSON,数组,SimpleXML和DOMDocument)的转换,以实现所需的结果。

我必须管理它有点hackish - 例如,一个当前的限制是,根据你的例子,值必须是数字(例如123或123.45) - 但在你的情况下可能就足够了。

$source =<<<XML
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
<root>
    <searcheddata>
        {Account0={AccountName=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0},  City1={City=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0},  State2={State=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, Zip=0.0, AlternateEmail=0.0},  Country3={Country=1, AddressLine2=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}}
    </searcheddata>
</root>
XML;

// Get rid of backslashes
$source = stripslashes($source);

// Create XML object, grab the data and remove whitespace
$xml  = new SimpleXMLElement($source);
$data = (string)$xml->searcheddata;
$data = preg_replace('/\s/', '', $data);

// Turn data into JSON:
// {foo={bar=1, baz=2.34}} => {"foo":{"bar":"1","baz":"2.34"}}
$data = preg_replace('/(\w+)=([\d\.]+)/', '"\1":"\2"', $data);
$data = preg_replace('/(\w+)=/',          '"\1":',     $data);

// Turn JSON into associative array
$decodedJson = json_decode($data, true);

// Function for adding array elements as XML child elements to XML element
function addElements(SimpleXMLElement $element, $a)
{
    foreach ($a as $key => $value) {
        if (is_array($value)) {
            $child = $element->addChild($key);
            addElements($child, $value);
        } else
            $element->addChild($key, $value);
    }
}

// Turn array into another Simple XML element and create <xml> and <root> tags
$simpleXml = new SimpleXMLElement('<xml/>');
$root      = $simpleXml->addChild('root');
addElements($root, $decodedJson);

// Pretty output by converting SimpleXML object to DOMElement
$dom = dom_import_simplexml($simpleXml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();

输出:

<?xml version="1.0"?>
<xml>
  <root>
    <Account0>
      <AccountName>1</AccountName>
      <AddressLine2>0.0</AddressLine2>
      <Country>0.0</Country>
      <CellPhone>0.0</CellPhone>
      <City>0.0</City>
      <WorkPhone>0.0</WorkPhone>
      <Other>0.0</Other>
      <HomePhone>0.0</HomePhone>
      <Email>0.0</Email>
      <AddressLine1>0.0</AddressLine1>
      <State>0.0</State>
      <Zip>0.0</Zip>
      <AlternateEmail>0.0</AlternateEmail>
    </Account0>
    <City1>
      <City>1</City>
      <AddressLine2>0.0</AddressLine2>
      <Country>0.0</Country>
      <CellPhone>0.0</CellPhone>
      <WorkPhone>0.0</WorkPhone>
      <Other>0.0</Other>
      <HomePhone>0.0</HomePhone>
      <Email>0.0</Email>
      <AddressLine1>0.0</AddressLine1>
      <State>0.0</State>
      <Zip>0.0</Zip>
      <AlternateEmail>0.0</AlternateEmail>
    </City1>
    ...
  </root>
</xml>