user1372430
user1372430

Reputation:

Change xml file string value from another xml file's content in php

My question may be easy. But I've never interested with XML. So it is not understandable for me. There is an installation program and it's language files are xml format. Turkish file is very narrow that other languages. So I want to import "turkish.xml" into "general.xml". In "general.xml" file, if there are same nodes, nodes value will be changed. If not, keep it same. For example this is content of my turkish.xml.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <DICTIONARY type="singlelanguage" lang="tr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd">
      <STRING id="AI.Directory.DefaultDefaultDir" value="Yeni Klasör"/>
      <STRING id="AI.Directory.Default32BitName" value="Otuziki bit"/>
    </DICTIONARY>

And below "general.xml" file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DICTIONARY type="singlelanguage" lang="neutral" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd">
  <STRING id="AI.Directory.DefaultDefaultDir" value="New Folder"/>
  <STRING id="AI.Directory.Default32BitName" value="32-bit"/>
  <STRING id="AI.DuplicateFile.DestName" value="Duplicate%s of %s"/>
  <STRING id="AI.Feature.DefaultTitle" value="Feature"/>
  </DICTIONARY>

If I manage it, it will be like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DICTIONARY type="singlelanguage" lang="neutral" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd">
  <STRING id="AI.Directory.DefaultDefaultDir" value="Yeni Klasör"/>
  <STRING id="AI.Directory.Default32BitName" value="Otuziki bit"/>
  <STRING id="AI.DuplicateFile.DestName" value="Duplicate%s of %s"/>
   <STRING id="AI.Feature.DefaultTitle" value="Feature"/>
  </DICTIONARY>

I do not know how I will manage it. Kind regards.

Upvotes: 0

Views: 717

Answers (2)

M8R-1jmw5r
M8R-1jmw5r

Reputation: 4996

I'm not sure if what you ask for is really useful because the software should already complete incomplete translation files with the defaults on the fly.

But sure technically it is possible, for example by going through the turkish XML and changing each corresponding entry in the general XML:

$new = simplexml_load_string($general);

$elements = simplexml_load_string($turkish)->STRING;

foreach ($elements as $element)
{
    $xpath = sprintf('//STRING[@id="%s"]', $element['id']);

    if (list($found) = $new->xpath($xpath))
    {
        $found['value']    = $element['value'];
        $found['imported'] = 'turkish';
    }
}

$new->asXML('php://output');

This example uses PHPs SimpleXML library. The output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DICTIONARY xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="singlelanguage" lang="neutral" xsi:noNamespaceSchemaLocation="dictionary.xsd">
  <STRING id="AI.Directory.DefaultDefaultDir" value="Yeni Klasör" imported="turkish"/>
  <STRING id="AI.Directory.Default32BitName" value="Otuziki bit" imported="turkish"/>
  <STRING id="AI.DuplicateFile.DestName" value="Duplicate%s of %s"/>
  <STRING id="AI.Feature.DefaultTitle" value="Feature"/>
</DICTIONARY>

See it in action.

Upvotes: 1

Piddien
Piddien

Reputation: 1446

You can try to load both files in to a DOMDocument, then loop through the general file. Inside the loop, you can see if there is a turkish value and just replace it. See http://www.php.net/manual/en/book.dom.php

I will just give it a try. I have not tested this code, but hopefully you will manage to fix it. Should be something like this.

EDIT I did not use the $result value in the loop, try now

    <?php
    $turkish = new DOMDocument();
    $turkish->load('turkish.xml');

    $general = new DOMDocument();
    $general->load('general.xml');   

    $strings = $general->getElementsByTagName('STRING');
    $turkstrings = $turkish->getElementsByTagName('STRING');
    foreach($strings as $string) {
        foreach($turkstrings as $turk) {
            if($turk->getAttribute("id") == $string->getAttribute("id")) {
                $string->setAttribute("value", $turk->getAttribute("value"));
            }
        }
    }
    echo $general->saveXML();
    ?>

Upvotes: 2

Related Questions