Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting XML to change all entries in xml file

    I have this xml but am struggling to get it to change all <item> elements it will only change the first enter in the xml string

    Original xml

    Code:
    <product>
    <item>
    <partno>abc123</partno><colour>pink</colour><price>10.99</price>
    <compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</compatbility>
    </item>
    <item>
    <partno>def123</partno><colour>pink</colour><price>10.99</price>
    <compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</compatbility>
    </item>
    </product>
    Changed XML

    Code:
    <product>
    <item>
    <partno>abc123</partno><colour>black</colour><price>10.99</price>
    <Compatbility><model>model1: 110C, 115C, 117C.</model><model>model2: 1835C, 1840C.</model><model>model3: 210C, 215C, 3240C.</model></Compatbility>
    </item>
    <item>
    <partno>abc123</partno><colour>black</colour><price>10.99</price>
    <Compatbility>not here model1: 110C, 115C, 117C. not here model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</Compatbility>
    </item>
    </product>
    The code i'm using with lots of commented out things that did not work

    $buffer = <<<XML
    <product>
    <item>
    <partno>abc123</partno><colour>pink</colour><price>10.99</price>
    <compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</compatbility>
    </item>
    <item>
    <partno>def123</partno><colour>pink</colour><price>10.99</price>
    <compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</compatbility>
    </item>
    </product>
    XML;

    Code:
    # load the xml string
    $xml = simplexml_load_string($buffer);
    
    # obtain the element in question
    foreach ($xml->item->compatbility as $compatbility);
    // foreach ($xml->item->compatbility as $compatbility);
    // $compatbility = $xml->item->compatbility;
    
    # parse it's inner text-value for the models by a regex
    $pattern = '~(model\\d?: [^.]+\\.) ?~u';
    $result  = preg_match_all($pattern, $compatbility, $matches);
    
    # remove the text (so called simplexml self-reference)
    
    $compatbility->{0} = ''; // this works on one row only
     
    // foreach ($xml->item->compatbility as $xml->item->compatbility->{0});
    // foreach ($xml->item->compatbility as $compatbility);
    // foreach ($compatbility as $compatbility) { $result(); }
    
    # add the parsed models as new model elements
    
    foreach ($matches[1] as $model) {
    	$compatbility->model[] = $model;
    	
    }
    
    // echo ($result); //total number of results
    //$output[] = $xml->item->compatbility as $compatbility;
    // echo $compatbility;
    
    # output the xml
    echo $compatbility->$xml->asXML('another.xml');
    I'm sure i'm just missing the plain simple. Any ideas as to what i've got wrong. thanks

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,228
    Thanks
    10
    Thanked 270 Times in 269 Posts
    PHP Code:
    foreach ($xml->item->compatbility as $compatbility); 
    ^
    That semicolon on the end is the termination of the foreach loop. I suspect you might not have known that, since you repeat the problem several times. As you want the loop to surround most of this code, you would use braces, like:
    PHP Code:
    foreach ($xml->item->compatbility as $compatbility)
    {
    blah


  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so stripping out all my notes removed ; still only changes first entry

    foreach ($xml->item->compatbility as $compatbility) // with ; removed

    $pattern = '~(model\\d?: [^.]+\\.) ?~u';
    $result = preg_match_all($pattern, $compatbility, $matches);

    $compatbility->{0} = '';

    foreach ($matches[1] as $model) {
    $compatbility->model[] = $model;

    }

    # output the xml to a new file
    $xml->asXML('another.xml');

    what goes in the blah section

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,637
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    Your code goes in the “blah” section. It must look like this:

    Code:
    foreach ($xml->item->compatbility as $compatbility) {
    
    $pattern = '~(model\\d?: [^.]+\\.) ?~u';
    $result = preg_match_all($pattern, $compatbility, $matches);
    
    $compatbility->{0} = '';
    
    foreach ($matches[1] as $model) {
    $compatbility->model[] = $model;
    
    }
    
    # output the xml to a new file
    $xml->asXML('another.xml');
    }

  • #5
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I tried it every where but after the $xml->asXML. But I'm still stuck and wonder as to why this will not loop over the xml and change the second - third - fourth enters. Lost I am... & baffled


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •