473,621 Members | 2,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding Attributes to all XML nodes

162 New Member
Howdy,

I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page.

This is the current code i am using to edit specific nodes.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $content = $_POST['content'];
  3. $id = $_POST['id'];
  4. $node = $_POST['node'];
  5. $explode = explode("_",$node);
  6.  
  7. $dom=new DOMDocument();
  8. $dom->load('sample.xml');
  9. $dom->formatOutput = true;
  10. //echo $dom->saveXML(); // show before file
  11. $allnodes = $dom->getElementsByTagName($explode[0]);
  12. foreach ($allnodes as $nodes) {
  13.     if ($nodes->nodeName==$explode[0] and $nodes->getAttribute('id')==$id) { //
  14.         $nodes->setAttribute($explode[1],$content);
  15.     }
  16. }
  17. $dom->save('sample.xml');
  18. ?>
  19.  
This is the XML file im working with. NOTE the missing *id* node of the 2nd *child01* node.
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <parent option01="other info" option02="other info" id="1">
  3.     <child01 option01="child01 option01" option02="child01 option02" id="2">
  4.         <child02 option01="child02 option01" option02="child02 option02" id="3">
  5.             <text option01="text option01" option02="text option02" id="4">
  6.                 <![CDATA[<b>Ma quande lingues coalesce</b>]]>
  7.             </text>
  8.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="5">image 1</image>
  9.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="6">image 2</image>
  10.         </child02>
  11.         <child02 option01="child02 option01" option02="child02 option02" id="7">
  12.             <text option01="text option01" option02="text option02" id="8">
  13.                 <![CDATA[Lorem ipsum dolor sit amet.]]>
  14.             </text>
  15.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="9">image 1</image>
  16.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="10">image 2</image>
  17.         </child02>
  18.     </child01>
  19.     <child01 option01="child01 option01" option02="child01 option02">
  20.         <child02 option01="child02 option01" option02="child02 option02">
  21.             <text option01="text option01" option02="text option02">
  22.                 <![CDATA[Epsum factorial non deposit quid pro quo hic escorol.]]>
  23.             </text>
  24.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  25.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  26.         </child02>
  27.         <child02 option01="child02 option01" option02="child02 option02">
  28.             <text option01="text option01" option02="text option02">
  29.                 <![CDATA[Li Europan lingues es membres del sam familie.]]>
  30.             </text>
  31.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  32.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  33.         </child02>
  34.     </child01>
  35. </parent>
  36.  
Jan 18 '08 #1
0 1361

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1975
by: louissan | last post by:
Hi all, I'm coming across a problem, and really do not get where it comes from. The goal: to loop over attributes read from "object" nodes in an imported XML file/flow (via XMLHTTP) and transform them into HTML/DOM attributes.
3
9908
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ========================= <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"
2
9297
by: Maersa | last post by:
hi, i'm trying to add "attributes" to some nodes and it works at design time but fails at runtime. XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement( "root" ); elem.InnerXml = "<first>abcde</first><last>1234</last>";
8
3244
by: fix | last post by:
Hi, I have an XML document and I need to add some attributes to an element. I use the XMLDocument object to load the xml file, and I can get the element I need to add attributes to in an XMLNode. The problem is, it seems that I cannot create a new XmlAttribute, it has no constructor. Does anyone have any idea how I can do this? I would appreciate your help. Thanks. fix.
6
10211
by: Hans Kamp | last post by:
My program fails reading XML attributes. A fragment of the code is: private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn) { int i; for (i = 0; i < xnl.Count; i++) { XmlNode xn = xnl; XmlNodeType nodeType = xn.NodeType;
8
3352
by: Ian Collins | last post by:
Is there a way to do this in IE? In Firefox, one can simply append a node from an XML return to the current document. IE whinges about 'No such interface supported' -- Ian Collins.
6
1892
by: Jakub.Bednarczuk | last post by:
Hallo everybody I have the problem with getting attributes values and also attributes names. I am reading an xml file with DOM. Lets see an example: file I read <root> <Def></Def> <Elements> <Element att1="1" att2="2" att3="3" //Some attributes are
1
1475
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page. This is the current code i am using to edit specific nodes. Im trying to use DOM, but it doesn't have to be. I just need it to work. <?php $content = $_POST; $id = $_POST; $node = $_POST;
12
18284
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the existing node with the new one. For example first xml file will be like; <Root> <Node Id ="1" FormId="form1" Value="value1"/> <Node Id ="2" FormId="form1" Value="value2"/> <Node Id ="3" FormId="form1" Value="value3"/>...
0
8212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8595
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7126
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5553
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.