Jump to content

Getting a whole xml node


watsmyname

Recommended Posts

Hello

I have xml like this.

<?xml version="1.0" encoding="utf-8"?>
<data>
   <item ID="30001">
      <Company>Navarro Corp.</Company>
   </item>
   <item ID="30002">
      <Company>Performant Systems</Company>
   </item>
   <item ID="30003">
      <Company>Digital Showcase</Company>
   </item>
</data>

 

All i need to do is get any one of the  whole node from starting item tag to ending item tag from the xml, for example.  getting

<item ID="30002">
      <Company>Performant Systems</Company> 
   </item>

from whole xml file.

 

Can this be achieved using  SimpleXML object from PHP? if not how it can be done.

 

 

Thanks in advance

watsmyname

Link to comment
Share on other sites

$str = '<?xml version="1.0" encoding="utf-8"?>
<data>
   <item ID="30001">
      <Company>Navarro Corp.</Company>
   </item>
   <item ID="30002">
      <Company>Performant Systems</Company>
   </item>
   <item ID="30003">
      <Company>Digital Showcase</Company>
   </item>
</data>';

$xml = new SimpleXMLElement($str);
foreach ($xml->children() as $node)
{
    // right now $node contains the SimpleXMLElement
    // but you can return it as a string with ->asXML()
    echo $node->asXML();
}

Link to comment
Share on other sites

$str = '<?xml version="1.0" encoding="utf-8"?>
<data>
   <item ID="30001">
      <Company>Navarro Corp.</Company>
   </item>
   <item ID="30002">
      <Company>Performant Systems</Company>
   </item>
   <item ID="30003">
      <Company>Digital Showcase</Company>
   </item>
</data>';

$xml = new SimpleXMLElement($str);
foreach ($xml->children() as $node)
{
    // right now $node contains the SimpleXMLElement
    // but you can return it as a string with ->asXML()
    echo $node->asXML();
}

well thanks for the reply but your code returns  " Navarro Corp.  Performant Systems  Digital Showcase  "

i want whole

 <item ID="30001">
      <Company>Navarro Corp.</Company>
   </item>

out of the given xml string, i.e.

including Item start tag to item end tag

Link to comment
Share on other sites

I'm not sure to be honest. ->asXML() doesn't have any formatting options so I'm not sure you'll be able to. They do mean the same thing though, why do you need it changing?

well the matter of fact is that a code reads an xml file, and each node is listed in each row of the  table through loop. and each time it puts a equivalent node in hidden text area. So first time in text area it puts

<item ID="30001">
      <Company>Navarro Corp.</Company>
   </item>

Second time it loops, in another text area it puts

<item ID="30002">
      <Company>Performant Systems</Company>
   </item>

and so on.  Actually this is done to delete a particular node. There is delete button in the last column of each row. When it is clicked, The value of corresponding text area is posted to next page. In next page again whole previous xml file is loaded as string. We match the value of text area to this string and replace it with blank space. and resulting string is saved as xml again.

 

Link to comment
Share on other sites

Why don't you just pass the item ID and remove the node using SimpleXML?

The given xml is just an example. There might be many tags with many attributes, and the amount of attributes on each node will be different and so are their values.

 

OKAY I SOLVED IT.  BUT IN A BIT LONG way.

Link to comment
Share on other sites

I'll be honest, it sounds like there should be a better way to do it. Perhaps you'd need to use the DOM for more advanced functionality?

 

 

OKAY I SOLVED IT.  BUT IN A BIT LONG way.

How did you solve it?

I used fread function to read an xml file. I put content in a string. I then exploded the string with </company>. I get each opening company tags with attributes in an array, i looped it and put each value [then concated </company> in array element] in the text area. And the text area is posted to another page. Rest i have already told. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.