trying to search an adobe flash xml file with xpath

150 views Asked by At

I am creating a web page to update an Adobe flash projects XML files. It's written in php using Simple XML and XPath. The only problem is that xPath will not search and files. I tried with a basic XML file and xPath works.

I am trying to find a specific node "<characters>" and replace it's concent.

Here is a sample XML file:

<DOMSymbolItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ns.adobe.com/xfl/2008/" name="Bt_Section manager" itemID="4725e7f8-000000db" symbolType="button" lastModified="1193667148">
  <timeline>
    <DOMTimeline name="Bt_Section manager">
      <layers>
        <DOMLayer name="Layer 1" color="#4F80FF" current="true" isSelected="true">
          <frames>
            <DOMFrame index="0" keyMode="9728">
              <elements>
                <DOMShape>
                  <fills>
                    <FillStyle index="1">
                      <SolidColor color="#FFDB62"/>
                    </FillStyle>
                  </fills>
                  <strokes>
                    <StrokeStyle index="1">
                      <SolidStroke scaleMode="normal" weight="2">
                        <fill>
                          <SolidColor/>
                        </fill>
                      </SolidStroke>
                    </StrokeStyle>
                  </strokes>
                  <edges>
                    <Edge fillStyle1="1" strokeStyle="1" edges="!809.5 0|5854 1.5!5854 1.5|6702 1188!6702 1188|0 1188!0 1188|809.5 0"/>
                  </edges>
                </DOMShape>
                <DOMStaticText selected="true" fontRenderingMode="standard" left="-112.25" width="141.55" height="46.7" isSelectable="false">
                  <matrix>
                    <Matrix a="0.754364013671875" d="0.746505737304688" tx="193.75" ty="14.1"/>
                  </matrix>
                  <textRuns>
                    <DOMTextRun>
                      <characters>Section manager</characters>
                      <textAttrs>
                        <DOMTextAttrs alignment="center" aliasText="false" autoKern="false" useScreenSpacing="true" size="20" face="Arial-BoldMT"/>
                      </textAttrs>
                    </DOMTextRun>
                  </textRuns>
                </DOMStaticText>
              </elements>
            </DOMFrame>
            <DOMFrame index="1" keyMode="9728">
              <elements>
                <DOMShape>
                  <fills>
                    <FillStyle index="1">
                      <SolidColor color="#FFDB62"/>
                    </FillStyle>
                  </fills>
                  <strokes>
                    <StrokeStyle index="1">
                      <SolidStroke scaleMode="normal" weight="2">
                        <fill>
                          <SolidColor/>
                        </fill>
                      </SolidStroke>
                    </StrokeStyle>
                  </strokes>
                  <edges>
                    <Edge fillStyle1="1" strokeStyle="1" edges="!809.5 0|5854 1.5!5854 1.5|6702 1188!6702 1188|0 1188!0 1188|809.5 0"/>
                  </edges>
                </DOMShape>
                <DOMStaticText selected="true" fontRenderingMode="standard" left="-112.25" width="141.55" height="46.7" isSelectable="false">
                  <matrix>
                    <Matrix a="0.754364013671875" d="0.746505737304688" tx="193.75" ty="14.1"/>
                  </matrix>
                  <textRuns>
                    <DOMTextRun>
                      <characters>Section manager</characters>
                      <textAttrs>
                        <DOMTextAttrs alignment="center" aliasText="false" autoKern="false" useScreenSpacing="true" size="20" face="Arial-BoldMT" fillColor="#FF6600"/>
                      </textAttrs>
                    </DOMTextRun>
                  </textRuns>
                </DOMStaticText>
             </elements>
            </DOMFrame>
          </frames>
        </DOMLayer>
      </layers>
    </DOMTimeline>
  </timeline>
</DOMSymbolItem>

I have tried with and without DOM.

Here is what I have tried with no results.

$xmlDoc=simplexml_load_file($XMLFile); 
$dom = new DOMDOcument();
$dom->loadXML($xmlDoc);
$xpath = new DOMXpath($dom);

echo "<pre>";

$result =   $xpath->query("//characters");              
      print_r($result);

echo "</pre>";

Thanks for any help,

Scott.

1

There are 1 answers

0
Shaunak Kashyap On

As you can see in the XML document, the characters nodes do not have a namespace prefix. This means they use the default namespace, which is defined in the root element by the xmlns attribute. In the given XML document, this default namespace is "http://ns.adobe.com/xfl/2008/".

You just need to register this default namespace before querying for any elements within it using XPath like so:

$xmlDoc=simplexml_load_file($XMLFile);

$xmlDoc->registerXPathNamespace("default", "http://ns.adobe.com/xfl/2008/");
$charactersNodes = $xmlDoc->xpath("//default:characters");

Note that I have used the word "default" above when registering this default namespace and again later when querying using XPath. This word could be anything, not necessarily "default".