Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 118843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:33:31+00:00 2026-05-11T03:33:31+00:00

My question is best phrase as: Remove a child with a specific attribute, in

  • 0

My question is best phrase as: Remove a child with a specific attribute, in SimpleXML for PHP

except I’m not using simpleXML.

I’m new to XML for PHP so I may not be doing the best way

I have a xml created using the $dom->save($xml) for each individual user. (not placing all in one xml due to undisclosed reasons)

It gives me that xml declaration <?xml version='1.0'?> (no idea how to make it to others, but that’s not the point, hopefully)

<?xml version='1.0'?> <details>  <person>name</person>  <data1>some data</data1>  <data2>some data</data2>  <data3>some data</data3>  <category id='0'>   <categoryName>Cat 1</categoryName>   <categorydata1>some data</categorydata1>  </category>  <category id='1'>   <categoryName>Cat 2</categoryName>   <categorydata1>some data</categorydata1>   <categorydata2>some data</categorydata2>   <categorydata3>some data</categorydata3>   <categorydata4>some data</categorydata4>  </category> </details> 

And I want to remove a category that has a specific attribute named id with the DOM class in php when i run a function activated from using a remove button.

the following is the debug of the function im trying to get to work. Can i know what I’m doing wrong?

function CatRemove($myXML){         $xmlDoc = new DOMDocument();         $xmlDoc->load( $myXML );         $categoryArray = array();          $main = $xmlDoc->getElementsByTagName( 'details' )->item(0);          $mainElement = $xmlDoc->getElementsByTagName( 'details' );         foreach($mainElement as $details){             $currentCategory = $details->getElementsByTagName( 'category' );             foreach($currentCategory as $category){                 $categoryID = $category->getAttribute('id');                 array_push($categoryArray, $categoryID);                 if($categoryID == $_POST['categorytoremoveValue']) {                     return $categoryArray;                 }             }         }           $xmlDoc->save( $myXML );     } 

Well the above prints me an array of [0]->0 all the time when i slot the return outside the if. is there a better way? I’ve tried using getElementbyId as well but I’ve no idea how to work that.

I would prefer not to use an attribute though if that would make things easier.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:33:32+00:00Added an answer on May 11, 2026 at 3:33 am

    Ok, let’s try this complete example of use:

    function CatRemove($myXML, $id) {     $xmlDoc = new DOMDocument();     $xmlDoc->load($myXML);     $xpath = new DOMXpath($xmlDoc);     $nodeList = $xpath->query('//category[@id=''.(int)$id.'']');     if ($nodeList->length) {         $node = $nodeList->item(0);         $node->parentNode->removeChild($node);     }     $xmlDoc->save($myXML); }  // test data $xml = <<<XML <?xml version='1.0'?> <details>  <person>name</person>  <data1>some data</data1>  <data2>some data</data2>  <data3>some data</data3>  <category id='0'>   <categoryName>Cat 1</categoryName>   <categorydata1>some data</categorydata1>  </category>  <category id='1'>   <categoryName>Cat 2</categoryName>   <categorydata1>some data</categorydata1>   <categorydata2>some data</categorydata2>   <categorydata3>some data</categorydata3>   <categorydata4>some data</categorydata4>  </category> </details> XML; // write test data into file file_put_contents('untitled.xml', $xml); // remove category node with the id=1 CatRemove('untitled.xml', 1); // dump file content echo '<pre>', htmlspecialchars(file_get_contents('untitled.xml')), '</pre>'; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure how best to phrase the question, but essentially I have a
(The title may not be proper but I try my best to phrase it)
I can't really think of the best way to phrase this question, so I'll
Not quite sure how to phrase this, but will do my best. I have
This may not be the most technical question, but I was just interested, nonetheless...
That's the best way I could think of to phrase my question, here is
I'm sure this is a common query but I'm not certain how to phrase
This question: Best way to return status flag and message from a method in
So this question is (somewhat distantly) related to this previous question: Best way to
Ok I nabbed method C for preloading images from this question best way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.