2014-05-20 97 views
-1

我在PHP中有一些字符串,我需要從它們製作XML。PHP從字符串創建XML

工作examle:

$xml = <<<EOT 
<?xml version="1.0"?> 
    <ALL> 
    <BLOCK id="1" > 
     <TEXT name="A1" /> 
     <TEXT name="A2" /> 
    </BLOCK> 
    <BLOCK id="2" > 
     <TEXT name="B1" /> 
     <TEXT name="B2" /> 
    </BLOCK> 
    </ALL> 
EOT; 

$dom = new DomDocument; 
$dom->preserveWhiteSpace = FALSE; 
$dom->loadXML($xml); 

我actualy已經和它不工作。 :

$string1 = '<TEXT name="A1" />'; 
$string2 = '<TEXT name="A2" />'; 
$string3 = '<TEXT name="B1" />'; 
$string4 = '<TEXT name="B2" />'; 

$xml = <<<EOT 
<?xml version="1.0"?> 
    <ALL> 
    <BLOCK id="1" > 
     $string 
     $string 
    </BLOCK> 
    <BLOCK id="2" > 
     $string3 
     $string4 
    </BLOCK> 
    </ALL> 
EOT; 

$dom = new DomDocument; 
$dom->preserveWhiteSpace = FALSE; 
$dom->loadXML($xml); 
+0

什麼不工作是什麼呢? – felipsmartins

回答

0

我想你忘了參考$字符串1和$字符串2瓦爾:

它爲我工作:

$string1 = '<TEXT name="A1" />'; 
$string2 = '<TEXT name="A2" />'; 
$string3 = '<TEXT name="B1" />'; 
$string4 = '<TEXT name="B2" />'; 

$xml = <<<EOT 
<?xml version="1.0"?> 
    <ALL> 
    <BLOCK id="1" > 
     $string1 
     $string2 
    </BLOCK> 
    <BLOCK id="2" > 
     $string3 
     $string4 
    </BLOCK> 
    </ALL> 
EOT; 


$dom = new DomDocument; 
$dom->preserveWhiteSpace = FALSE; 
$dom->loadXML($xml); 

echo $dom->saveXML();