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

  • Home
  • SEARCH
  • 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 7049731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:03:26+00:00 2026-05-28T03:03:26+00:00

How can I delete specific elements on xml using php my.xml <usr> <uid trk=

  • 0

How can I delete specific elements on xml using php

my.xml

    <usr>
        <uid trk= "1234">
            <deleteThis>
                <mychild>here</mychild>
                <anotherchild>here</anotherchild>
            </deleteThis>
        </uid>
    </usr>

I want to remove the “deleteThis” element and its children

result:

     <usr>
        <uid trk= "1234">
        </uid>
    </usr>

here’s my non-working code

index.php

$xml = new DOMDocument; 
    $xml->load('my.xml');
    $thedocument = $xml->documentElement;
    $list = $thedocument->getElementsByTagName('uid');

    foreach ($list as $domElement){
      $attrValue = $domElement->getAttribute('trk'); 
      if ($attrValue == "1234") { //if <uid trk= "1234">
        $valY = $domElement->getElementsByTagName('deleteThis');
        $thedocument->removeChild($valY);
      }
    }
$xml->save("my.xml");

It seems it doesn’t found the node.

  • 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. Editorial Team
    Editorial Team
    2026-05-28T03:03:27+00:00Added an answer on May 28, 2026 at 3:03 am
      if ($attrValue == "1234") { //if <uid trk= "1234">
        $valY = $domElement->getElementsByTagName('deleteThis');
        //$valY is a DOMNodeList, that you happen to know there is only one doesnt matter
        foreach($valY as  $delnode){
          $delnode->parentNode->removeChild( $delnode);
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I delete specific DataRows within a loop of a DataTable rows which
I'm using PHP's DOMDocument and related classes to work with XML. The XML contains
Is there a way I can delete all files from svn having a specific
how can i delete all CSS-stylings in one skid from a specific Selector. Is
I am using Emacs 23.3 on Mac OS X Lion. How can I delete
I can delete files with specific extensions in multiple folders with this: Get-childitem *
How can I delete a specific row from an sql table? I have a
MSDN displays the following for CreatePatternBrush: You can delete a pattern brush without affecting
I'm looking for a code snippet that can delete all emails and text messages
I'm trying to write a windows batch file that can delete files from subdirectories.

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.