Jump to content

Xml Add, Edit And Comment Out Nodes And Attributes


kvnmck18
 Share

Recommended Posts

It has been a good couple of years since I have used SimpleXML to add and modify nodes/attributes. For a personal project I am working on it would be the easiest solution. Below I have posted an example of my XML. The XML is used to build forms for an little jQuery web-app. I am trying to use PHP to create new <Type/> with an inner child of <form/> that will have different <input/> nodes that will have different content in their attributes. Basically, it's pretty simple - I just need some refreshing on how to do this. The only "tricky" part is if a node is added with a the @type="select" it will have it's own children (<input/>).I just need a simple way to create these nodes/attributes and modify them. It's sad that php.net documentation is horrible on this topic and you have to dig deep into XML DOM to even get started.Another tricky thing is that I do not want to delete nodes, the "delete function" I want to work as a "comment out" for that node. So wrap that node with a "<!-- <node/> -->"

<Site>  <Page>	<Type title="Web form" description="Create a new form">	  <form name="webpageFORM">		<input title="Question" type="checkbox" defaultValue="1"/>		<input title="Email" type="input" defaultValue=""/>		<input title="Select" type="select" defaultValue="">		  <input title="Opt 1" defaultValue="1"/>		  <input title="Opt 2" defaultValue="2"/>		  <input title="Opt 3" defaultValue="3"/>		</input>	  </form>	</Type>  </Page></Site>

Hope someone can help me out. Let me know if I didn't explain this good enough // I'm pretty tired.Thanks in advance!-Kevin

Link to comment
Share on other sites

The trick is the addChild() function. You need to go down to the insertion point (the appropriate <Page/> element), call addChild() to create an element at that point, and then move to that element, adding the respective child elements and their attributes in.The commenting out part is harder though. I don't know if SimpleXML can do that alone. You could use asXML() on the node you want to comment out, then read the document as DOM, find the same node again, create a comment node next to it (with insertBefore()), add the asXML() representation as that node's content, and remove the real node.

Edited by boen_robot
Link to comment
Share on other sites

I feel that you and I worked on one a couple years back. Do you remember this? If so, do you know where that forum thread is?

Link to comment
Share on other sites

I feel that you and I worked on one a couple years back. Do you remember this? If so, do you know where that forum thread is?
Um... no... sorry. I don't recall... I only remember a pagination deal, and a "generic XML editor" deal. We might have discussed a similar thing with PMs, but I certainly don't recall that.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...