473,785 Members | 2,811 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 1368

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

Similar topics

4
1982
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
9927
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
9309
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
3248
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
10216
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
3366
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
1901
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
1488
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
18322
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
9645
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
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.