PHP XML losing attributes -
I have a problem when I load the XML string in simple XML (I also tried it with DOCDocument but the result is same is). I have the following in XML:
& lt; Define Name & gt; & Lt; Defined name name = "name1" id = "1" hidden = "1" & gt; NAME_TEST & lt; / DefinedName & gt; & Lt; Defined name name = "name2" id = "4" hidden = "1" & gt; NAME_TEST_2 & lt; / DefinedName & gt; & Lt; / DefinedNames & gt;
Now I need access to specific tags using the 'name' attribute. But always when I try print_r, var_dump or smth other I always see all other attributes, But when I
[0] = & gt; NAME_TEST, [1] = & gt; NAME_TEST_2
I also tried xpath, but whenever I refer to the properties, I get an empty table.
So now I tried: xpath, simplexmldom, docdocument but the result is always the same - the empty array any clue?
@edit
$ xl-> LoadTemplate ('# xl / workbook.xml'); If (isset ($ workbook) and & is_array ($ workbook) & calculation ($ workbook & gt; 0)) {$ dom = new DOMDocument (); $ Dom- & gt; LoadXML ($ xl- & gt; source); $ Xpath = new DOMXpath ($ dom); Forex Currency ($ xpath-> Evaluate ('// defined name') $ as defined name) {echo $ define-name-> GetAttribute ('name'); }} And {$ TBS- & gt; Source = preg_rele ('~ \ & lt; defined name' gt; * \ & lt; \ / defined name & gt; ~ ',' ', $ TBS- & gt; source); }
@ edit2 - xml
& lt ;? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"? & Gt; & Lt; Workbook xmlns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns: r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns: MC = "http: // Schemas.openxmlformats.org/markup-compatibility/2006 "MC: Ignorable =" x15 "xmlns: x15 =" http://schemas.microsoft.com/office/spreadsheetml/2010/11/main "& gt; & Lt; DefinedNames & gt; & Lt; Defined name name = "name1" id = "1" hidden = "1" & gt; NAME_TEST & lt; / DefinedName & gt; & Lt; Defined name name = "name2" id = "4" hidden = "1" & gt; NAME_TEST_2 & lt; / DefinedName & gt; & Lt; / DefinedNames & gt; & Lt; / Workbook & gt;
I know that is like smth but I've already tried:
$ xpath-> Evaluate ('// workbook / definedNames / definedName [*]')
or
$ xpath- & gt; Rate (
"
With DOMDocument, you use Xpath to fetch values or nodes.
Load XML in a document and make a DOMXpath example for it:
$ dom = new DOMDocument (); $ Dom- & gt; LoadXml ($ XML); $ Xpath = new DOMXpath ($ dom);
Get a node value in the form of a string:
var_dump ($ xpath-> evaluate ('string (// defined name [ @ Name = "name1"]])););
Output:
string (9) "NAME_TEST"
All defined name element data in an array Fetch:
$ Dahrenam = []; Forex Currency ($ xpath-> Evaluate ('// defined name') $ as defined name) {name by $ [] = ['name' = & gt; $ Define name- & gt; GetAttribute ('name'), 'id' = & gt; $ Define name- & gt; GetAttribute ('id'), 'hidden' = & gt; $ Define name- & gt; GetAttribute ('hidden'), 'text' = & gt; $ Define name- & gt; Nodevalue]; } Var_dump ($ byName);
Output:
array (2) {[0] = & gt; Array (4) {["name"] = & gt; String (5) "name1" ["id"] = & gt; String (1) "1" ["hidden"] = & gt; String (1) "1" ["text"] = & gt; String (9) "NAME_TEST"} [1] = & gt; Array (4) {["name"] = & gt; String (5) "name2" ["id"] = & gt; String (1) "4" ["hidden"] = & gt; String (1) "1" ["text"] = & gt; String (11) "NAME_TEST_2"}}
Comments
Post a Comment