473,785 Members | 2,989 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. Im trying to use DOM, but it doesn't have to be. I just need it to work.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $content = $_POST['content'];
  4. $id = $_POST['id'];
  5. $node = $_POST['node'];
  6. $explode = explode("_",$node);
  7.  
  8. $dom=new DOMDocument();
  9. $dom->load('sample.xml');
  10. $dom->formatOutput = true;
  11. //echo $dom->saveXML(); // show before file
  12. $allnodes = $dom->getElementsByTagName($explode[0]);
  13. foreach ($allnodes as $nodes) {
  14.     if ($nodes->nodeName==$explode[0] and $nodes->getAttribute('id')==$id) { //
  15.         $nodes->setAttribute($explode[1],$content);
  16.     }
  17. }
  18. $dom->save('sample.xml');
  19. ?>
  20.  
This is the XML file im working with. **NOTE** the missing *id* attributes of the 2nd *child01* node.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <parent option01="other info" option02="other info" id="1">
  4.     <child01 option01="child01 option01" option02="child01 option02" id="2">
  5.         <child02 option01="child02 option01" option02="child02 option02" id="3">
  6.             <text option01="text option01" option02="text option02" id="4">
  7.                 <![CDATA[<b>Ma quande lingues coalesce</b>]]>
  8.             </text>
  9.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="5">image 1</image>
  10.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="6">image 2</image>
  11.         </child02>
  12.         <child02 option01="child02 option01" option02="child02 option02" id="7">
  13.             <text option01="text option01" option02="text option02" id="8">
  14.                 <![CDATA[Lorem ipsum dolor sit amet.]]>
  15.             </text>
  16.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="9">image 1</image>
  17.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="10">image 2</image>
  18.         </child02>
  19.     </child01>
  20.     <child01 option01="child01 option01" option02="child01 option02">
  21.         <child02 option01="child02 option01" option02="child02 option02">
  22.             <text option01="text option01" option02="text option02">
  23.                 <![CDATA[Epsum factorial non deposit quid pro quo hic escorol.]]>
  24.             </text>
  25.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  26.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  27.         </child02>
  28.         <child02 option01="child02 option01" option02="child02 option02">
  29.             <text option01="text option01" option02="text option02">
  30.                 <![CDATA[Li Europan lingues es membres del sam familie.]]>
  31.             </text>
  32.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  33.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  34.         </child02>
  35.     </child01>
  36. </parent>
  37.  
Jan 21 '08 #1
1 1488
jkmyoung
2,057 Recognized Expert Top Contributor
Have an addition to what I said in the other forum:

It would be easier to do this in xslt:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="*">
  2.   <xsl:copy>
  3.     <xsl:copy-of select="@*[not (name() = 'id')]"/>
  4.     <xsl:attribute name="id" select="count(preceding::*) +1"/>
  5.   </xsl:copy>
  6. </xsl:template>
  7.  
The hard part might be setting this up in php if you're not familiar with xslt. For that look here:
http://www.tonymarston.net/php-mysql/xsl.html#a6
Jan 21 '08 #2

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
0
1369
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. <?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
10330
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
10093
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
9952
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...
0
8976
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
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
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.