Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Adding Attributes to all XML Nodes
Old 01-21-2008, 02:41 AM Adding Attributes to all XML Nodes
Extreme Talker

Posts: 199
Trades: 0
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.
Code:
 
<?php
$content = $_POST['content'];
$id = $_POST['id'];
$node = $_POST['node'];
$explode = explode("_",$node);
 
$dom=new DOMDocument();
$dom->load('sample.xml');
$dom->formatOutput = true;
//echo $dom->saveXML(); // show before file
$allnodes = $dom->getElementsByTagName($explode[0]);
foreach ($allnodes as $nodes) {
    if ($nodes->nodeName==$explode[0] and $nodes->getAttribute('id')==$id) { //
        $nodes->setAttribute($explode[1],$content);
    }
}
$dom->save('sample.xml');
?>
This is the XML file im working with. **NOTE** the missing *id* attributes of the 2nd *child01* node.
Code:
 
<?xml version="1.0" encoding="utf-8"?>
<parent option01="other info" option02="other info" id="1">
    <child01 option01="child01 option01" option02="child01 option02" id="2">
        <child02 option01="child02 option01" option02="child02 option02" id="3">
            <text option01="text option01" option02="text option02" id="4">
                <![CDATA[<b>Ma quande lingues coalesce</b>]]>
            </text>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="5">image 1</image>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="6">image 2</image>
        </child02>
        <child02 option01="child02 option01" option02="child02 option02" id="7">
            <text option01="text option01" option02="text option02" id="8">
                <![CDATA[Lorem ipsum dolor sit amet.]]>
            </text>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="9">image 1</image>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="10">image 2</image>
        </child02>
    </child01>
    <child01 option01="child01 option01" option02="child01 option02">
        <child02 option01="child02 option01" option02="child02 option02">
            <text option01="text option01" option02="text option02">
                <![CDATA[Epsum factorial non deposit quid pro quo hic escorol.]]>
            </text>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
        </child02>
        <child02 option01="child02 option01" option02="child02 option02">
            <text option01="text option01" option02="text option02">
                <![CDATA[Li Europan lingues es membres del sam familie.]]>
            </text>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
            <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
        </child02>
    </child01>
</parent>
empiresolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-21-2008, 05:26 AM Re: Adding Attributes to all XML Nodes
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,371
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
more of a PHP question, moving thread
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-21-2008, 12:49 PM Re: Adding Attributes to all XML Nodes
Extreme Talker

Posts: 199
Trades: 0
Quote:
Originally Posted by chrishirst View Post
more of a PHP question, moving thread
PHP people say this is belongs more in XML. I had posted this to the PHP forum with no response.
empiresolutions is offline
Reply With Quote
View Public Profile
 
Old 01-21-2008, 02:42 PM Re: Adding Attributes to all XML Nodes
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,371
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok, but as you are asking about a PHP editor that creates the XML file
Quote:
I have an PHP page that edits XML files
I would assume you are looking for some PHP code.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-21-2008, 03:20 PM Re: Adding Attributes to all XML Nodes
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,371
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
take a look at Thierry's code class in

http://www.webmaster-talk.com/php-fo...unce-make.html
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-21-2008, 04:27 PM Re: Adding Attributes to all XML Nodes
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I don't get it....
Quote:
I want ADD a new *id* attribute to all nodes on the page that do not have it all ready
Until there, ok.

but...
Quote:
Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page
So, you just added ID's where they where missing, and now you want to reset it to an arbitrary value ?
And you want every id's to be at the same value; namely the count of elements in the document ???
Or you just want to number them sequentially ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-21-2008, 04:36 PM Re: Adding Attributes to all XML Nodes
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok, if you just want to add id attributes to every nodes, and to have them numbered sequentially:
PHP Code:
$dom=new DOMDocument();
$dom->load('sample.xml');
$dom->formatOutput true;
//echo $dom->saveXML(); // show before file
$allnodes $dom->getElementsByTagName("*");
$maxId=1;
foreach (
$allnodes as $node) {
    
$node->setAttribute('id',$maxId);
    
$maxId++;
}
$dom->save('sample.xml');
echo 
$dom->saveXML(); 
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-21-2008, 04:44 PM Re: Adding Attributes to all XML Nodes
Extreme Talker

Posts: 199
Trades: 0
tripy -

thanks for your suggestion. i guess what i meaning to do is add an *id* attribute to those nodes that dont have one. then when i add a new node, i want to be able top re-number them done the page.

chrishirst - thanks for Thierry's link.
empiresolutions is offline
Reply With Quote
View Public Profile
 
Old 01-21-2008, 05:14 PM Re: Adding Attributes to all XML Nodes
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Then no need to add and then remove.
You start at 1, and you re-set on each elements.

Those who don't have an id will get one, those with an id will have their value refreshed.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-25-2008, 03:21 AM Re: Adding Attributes to all XML Nodes
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
Right, tripy's code should work. I have to note that you don't want to add the attribute to all Nodes, but all Elements.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Reply     « Reply to Adding Attributes to all XML Nodes
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.79260 seconds with 12 queries