Pregunta

Hey i am looking to make a copy of an existing XML and add a new attribute to the top of it

My current xml is something like:

<?xml version="1.0"?>
<export>
  <config>
    <Exported Name="test">
      <values>
        <node name="yellow" />
        <node name="green" />
        <node name="red" />
      </values>
  </config>
</export>

and i just want to add an email field, i am conforming it to match with specs so i dont have control over changing the layout so i cant do for example

 <email value="myemail@gmail.com"></email>

so i am stuck to having it displayed as below

<?xml version="1.0"?>
<email>
  myemail@gmail.com
</email>
<export>
  <config>
    <Exported Name="test">
      <values>
        <node name="yellow" />
        <node name="green" />
        <node name="red" />
      </values>
  </config>
</export>

I have tried a bunch of things but i am struggling. tbh i cant figure out a way to just add the value of the email inside the email node, all the examples i found seem to tell you how to add an attribute and then a child inside it but i couldnt find an example to add just a value inside of email node.

What i was trying as a first step was just to have it add the attribute email so i could at least get that part working but i am also failing at that. Of course my ultimate aim is to get the email inside the node email but i am just showing this as an example of what i tried and failed at. I am using the php documentation example but i obviously am doing something wrong cause i get an error: String could not be parsed as XML

$xml_file = "testing.xml";
$xml_path = XML_PATH.$xml_file;

$add_email = new SimpleXMLElement($xml_path);
$add_email->addAttribute('address', 'myemail@gmail.com');
$email = $add_email->addChild('email');

echo '<PRE>';
print_r($add_email);
echo '</PRE>';

However if i do the following i can see the xml echoed fine so the path etc is fine, its how im structuring the code is where the problem is. Any hints on where im messing up?

$xml_file = "testing.xml";
$xml_path = XML_PATH.$xml_file;

$xml = simplexml_load_file($xml_path);
echo '<PRE>';
print_r($add_email);
echo '</PRE>';

The above outputs the xml fine.

¿Fue útil?

Solución

As mentioned in comments, you are trying to create invalid XML and therefore the SimpleXML extension is of little use here. You need to use normal string manipulation. Here is a fragile (because it is matching on ?>) implementation:

<?php
$xml_file = "testing.xml";
$xml_path = XML_PATH . $xml_file;

$contents = file_get_contents($xml_path);
$contents = str_replace("?>", "?>\n<email>myemail@gmail.com</email>", $contents);

print_r($contents);
?>

Keep in mind that trying to pass the resulting XML into anything that expects valid XML will fail horribly.

The real question remains: why would you knowingly want to generate invalid XML in the first place?

Edit: To answer your comment from below:

Can someone explain what is happening here with the ?> is that just hidden text in an XML file at the start of each line?

No, it is not hidden, the ?> is right here:

<?xml version="1.0"?>
                   ^^
<export>
  ....
</export>

The PHP code that I added replaces that ?> with ?> a new line (\n) and the XML node that you wanted.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top