php - simpleXML how to add a new attribute to existing XML -


Hey I'm trying to make a copy of an existing XML and add a new feature to it. >

My current XML is something like this:

  & lt ;? xml version = "1.0"? & Gt; & Lt; Export & gt; & Lt; config & gt; & Lt; Exported name = "test" & gt; & Lt; Values ​​& gt; & Lt; Node name = "yellow" /> & Lt; Node name = "green" /> & Lt; Node name = "red" /> & Lt; / Values ​​& gt; & Lt; / Config & gt; & Lt; / Export & gt;   

And I just want to add an email field, I agree to match it with glasses so I do not control the layout change so I can not do it for example < / P>

  & lt; Email value = "myemail@gmail.com" & gt; & Lt; / Email & gt;   

So I'm stuck to display it as below

   & Lt; Node name = "green" /> & Lt; Node name = "red" /> & Lt; / Values ​​& gt; & Lt; / Config & gt; & Lt; / Export & gt;   

I have tried many things but I am struggling. Tbh I understand how to add the value of email inside the email node, all the examples I have given you tell me how to add an attribute and inside it is a child, but I am just an email node inside Values.

What I was trying to do as a first step was to just add the feature email, so I could at least have worked that part, but I failed in that too. Of course, my ultimate purpose is to get email inside the node email, but I'm just showing it as an example of what I tried and failed. I am using the PHP documentation example, but obviously I am doing some wrong reason because I get an error: the string can not be parsed as XML

  $ xml_file = "Test.xml"; $ Xml_path = XML_PATH. $ Xml_file; $ Add_email = New SimpleXMlelement ($ xml_ path); $ Add_email- & gt; addAttribute ('address', 'myemail@gmail.com'); $ Email = $ add_email- & gt; Add Child ('Email'); Echo '& lt; PRE & gt; '; Print_r ($ add_email); Echo '& lt; / PRE> ';   

However, if I do the following, I can see that XML is properly echoed, so the path is fine, this is how it structures the incoming code where there is a problem where the signal is IM messing up?

  $ xml_file = "test.xml"; $ Xml_path = XML_PATH. $ xml_file; $ Xml = Simple XMLload_file ($ xml_ path); Echo '& lt; PRE & gt; '; Print_r ($ add_email); Echo '& lt; / PRE> ';   

Fixes the above XML.

As noted in the comments, you are trying to create an invalid XML and therefore the simple extension extension There is little use here. You need to use normal string manipulation. It's a delicate (because it is ? ):

   \ N   

Keep in mind that anything that the resulting XML expects from a valid XML will be very unsuccessful.

The real question is: Why do you want to intentionally generate invalid XML in the first place?

Edit: To reply to your comment from below:

Can anyone say what is happening here? & Gt; Is there a hidden text in XML file at the beginning of each line?

No, it is not hidden, ? & Gt; Here's right:

  & lt ;? Xml version = "1.0"? & Gt; ^^ & lt; Export & gt; .... & lt; / Export & gt;   

The code that I changed that PHP code? & Gt; with ? & Gt; a new row ( \ n ) and the XML node you wanted.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -