0

I have some string in PHP, and I need to make XML from them.

Working 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);

What I actualy have and it does not work. :

$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);
1
  • What is not working exactly? Commented May 20, 2014 at 19:18

1 Answer 1

1

I think you've forgotten to refer $string1 and $string2 vars:

It work for me:

$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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.