-1

I'm building an application on Zend Framework and now there is requirement to manipulate a xml file and change few content of it so that I could use the xml as a request file to get a response from other server. Now after a googling I've seen popularity of simplexml over others but I'm not sure which implementation would be better for Zend Framework. Any suggestions would be very helpful. My request XML file is like this:

<?xml version="1.0" encoding="UTF-8"?>
    <aa>
        <bb>
            <cc>
                <dd>
                    <ee/>
                </dd>
                <ff>
                    <Identification _FirstName="******" _LastName="*****" _Num="********">
                        <gg/>
                    </Identification>
                </ff>
            </cc>
        </bb>
    </aa>

Now, I need to be able to change the value of _FirstName, _LastName and Num. Which XML manipulation method should I use with Zend Framework for this and how?

Thanks in advance.

hakre
  • 193,403
  • 52
  • 435
  • 836
Prakash
  • 109
  • 1
  • 7

2 Answers2

1

Zend Framework provides the Zend_DOM library, which builds upon PHP's native DOM extension (linkage mine):

Zend_Dom provides tools for working with DOM documents and structures. Currently, we offer Zend_Dom_Query, which provides a unified interface for querying DOM documents utilizing both XPath and CSS selectors.

If you don't need Selectors, you don't need Zend_DOM, but can use any of the native XML extensions.

How to use Zend_DOM is explained in the reference guide:

To use Zend_Dom_Query, you instantiate a Zend_Dom_Query object, optionally passing a document to query (a string). Once you have a document, you can use either the query() or queryXpath() methods; each method will return a Zend_Dom_Query_Result object with any matching nodes.

So in a nutshell:

$dom = new Zend_Dom_Query($xmlString);
$results = $dom->query('Identification');
$identification = $results->current();

$identification will then contain a DOMElement which you can change like any other DOMElement. There is ample examples on StackOverflow on how to change values in an XML file, so I won't repeat that here.

After you changed the DOMElement, you need to get hold of the DOMDocument to serialize the changed document back into XML. To do so, you can use

$doc = $identification->ownerDocument;
$doc->save('filename.xml'); // to save it to a file
$xmlString = $doc->saveXML(); // to save it to a variable

For an overview of X(HT)ML parsers see

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Hey Gordon, Thanks for the reply. I'll be using Zend_DOM library for this. – Prakash Jun 14 '12 at 07:04
  • I don't think that the linked CSS Selectors standard is really supported in `Zend_DOM`. Smells a bit like marketing. – hakre Jun 14 '12 at 13:09
  • @hakre not sure to which extend they support selectors. the linkage in the quote is mine. I really just wanted to give a link to the standard here. – Gordon Jun 14 '12 at 13:14
0

It is better to use the following structure:

<Identification>
     <FirstName>*****</FirstName>
     <LastName>*****</LastName>
     <Num>*****</Num>
   </Identification>

And use Zend_Config_Xml class: http://framework.zend.com/manual/en/zend.config.adapters.xml.html

vedmed
  • 255
  • 2
  • 7
  • 16