[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Query XML for a Node Attribute value and delete the Node

Posted on 2009-02-23
1
Medium Priority
?
2,493 Views
Last Modified: 2013-11-10
I need some help identifying the best way to Query an XML file, Fild the correct 'issue' node based on the acc_num attribute, then delete the entire issue node that the acc_num belongs too.

I have tried both simpleXML and DOM and have not figured out a way that works.  When I run the code below it never seems to find the correct acc_num.

Anyone have any ideas or a better example to accomplish the same thing?

Help is appreciated


<issues>  
<issue acc_num="5950001">
    <username>user1</username>
    <password>user1</password>
    <acc_num>5950001</acc_num>
    <pat_num>S009000</pat_num>
    <study_date>February 18, 2009</study_date>
    <sup_name>super</sup_name>
    <sup_nuid>00004</sup_nuid>
    <modality_name>Bmc</modality_name>
    <severity>3</severity>
    <issue_title>QC-MARKER</issue_title>
    <issue_desc>
TESTing layout 2 </issue_desc>
  </issue>
  <issue acc_num="5950002">
    <username>user1</username>
    <password>user1</password>
    <acc_num>5950002</acc_num>
    <pat_num>S001000</pat_num>
    <study_date>February 18, 2009</study_date>
    <sup_name>super</sup_name>
    <sup_nuid>00005</sup_nuid>
    <modality_name>MRc</modality_name>
    <severity>3</severity>
    <issue_title>QC-ARTIFACT</issue_title>
    <issue_desc>TESTing layout 3</issue_desc>
  </issue>
</issues>
 
**************  Current Code  ***********************
        $xmlfile =  'external/external_post.xml';  
                
        $xmlDoc = new DomDocument; 
        $xmlDoc->validateOnParse = true; 
        $xmlDoc->Load($xmlfile); 
        $xmlDoc->preserveWhiteSpace = FALSE; 
        $xpath = new DOMXpath($xmlDoc);
 
        
         
             foreach($validData as $data)  {
              //Keep the bad data in the file untill it can be fixed   
                  $gooddata_holder = $data;
                  $nodelist = $xpath->query('//issue[@acc_num="' . $data . '"]');
                                                                              
                    if ($nodeList->length) {
                        $node = $nodeList->item(0);
                        $node->parentNode->removeChild($node);
                    }
              }
 
$dom->save($xmlfile) or die ("Error saving to file");

Open in new window

0
Comment
Question by:jrlitm
1 Comment
 

Accepted Solution

by:
jrlitm earned 0 total points
ID: 23715739
** Resolved **

To any interested... I attached the resolution
        $xmlfile =  'external/external_post.xml';  
                
        $xmlDoc = new DomDocument; 
        $xmlDoc->validateOnParse = true; 
        $xmlDoc->Load($xmlfile); 
 
        $xmlDoc->preserveWhiteSpace = FALSE; 
 
             foreach($validData as $data)  {
               
                 $gooddata_holder = $data;                
                 $root = $xmlDoc->documentElement;
                      
                 if ( $children = $root->getElementsByTagName("issue")) {
                      foreach ($children as $child)     {
                          Echo  $child->getAttribute("acc_num") . "/n";
                           if ( $child->getAttribute("acc_num") == $data ) {
                                $root->removeChild($child);
                                Echo  $child->getAttribute("acc_num") . "/n"; 
                                Continue 2;
                           }
                      }     
                 }
             }

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question