473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Adding Attributes to all XML Nodes

162 100+
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 1459
jkmyoung
2,057 Expert 2GB
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
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...
3
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 ...
2
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" );...
8
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...
6
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 =...
8
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
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>...
0
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...
12
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.