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

Ask A Question

Stats

  • Questions 98k
  • Answers 99k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So it turns out that I was looking for the… May 11, 2026 at 7:40 pm
  • Editorial Team
    Editorial Team added an answer So if the user doesn't know their username, they provide… May 11, 2026 at 7:40 pm
  • Editorial Team
    Editorial Team added an answer I would go with option 3, and make the slug… May 11, 2026 at 7:40 pm

Related Questions

How do I distribute a small amount of data in a random order in
My question concerns c# and how to access Static members ... Well I don't
It seems there is a new catch-phrase emerging in the web development field: object-oriented
I have been tasked to write a device driver for an embedded device which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.