简体   繁体   中英

XML node content change

I am having a xml content like

<?xml version="1.0"?>     
              <content
                  ID="immunSect"/>
                  <table
                     border="1"
                     width="100%">
                     <thead>
                        <tr>
                           <th>Vaccine</th>
                           <th>Date</th>
                           <th>Status</th>
                        </tr>
                     </thead>
                     <tbody>
                        <tr>
                           <td><content
                              ID="immun2"/>Influenza virus vaccine</td>
                           <td>May 2012</td>
                           <td>Completed</td>
                        </tr>
                        <tr>
                           <td><content
                              ID="immun4"/>Tetanus and diphtheria toxoids</td>
                           <td>April 2012</td>
                           <td>Completed</td>
                        </tr>
                     </tbody>
                  </table>
               </text>

My problem is I would like to change this node

<tbody>
                        <tr>
                           <td><content
                              ID="immun2"/>Influenza virus vaccine</td>

into

<tbody>
                            <tr>
                               <td><content
                                  ID="immun2">Influenza virus vaccine</content</td>

Please help me how can I fetch that particular section and change the node structure from

<td><content id="xxx"/>test

into

<td><content id="xxx">test</content>

Basically when geting XML data, put loop until EOF in which get each line (@string1) and parse it, rewriting it to new variable (@string2), which you can save it to XML file/whatever you need with it. Start parsing @string1, moving everything to @string2, until you encounter and ignore it, instead put a flag to 1. Continue with copying, until you find then input into @string2 . Of course it can be done in more elegant way, but I would need to know what are you using to get XML data.

Hope that helps.

Since your XML is malformed, you can't use parsing tools such as DOMDocument. Probably your only choice is a regex replacement using preg_replace . This should work:

$xml = preg_replace('#<td>(<content.*?)</td>#s', '<td>$1</content></td>', $xml);

Demo on 3v4l.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM