I used HTTPService for reading xml, but I want only a particular xml field value like the
first node id.
The HTTPService object is:
<mx:HTTPService result="getid(event)" id="xml_coupon" url="###" useProxy="false" resultFormat="e4x"/>
The getid(event) function is:
public function getid(evt:ResultEvent):void
{
var id:number=evt.result.id;
Alert.show(id.tostring);
}
The getid function shows all ids, but I want the first index id only. How can I read this? I tried Alert.show(evt.getChildAt(1).id); but it shows an error. If you know, please help me.
hey do some thing like this
if xml is like this
<mx:XML id="usersXML">
<root>
<users>
<user id="1" lovesDonuts="Yes">
<firstname>Tariq</firstname>
<lastname>Ahmed</lastname>
</user>
<user id="2" lovesDonuts="Yes">
<firstname>Jon</firstname>
<lastname>Hirschi</lastname>
</user>
</users>
</root>
</mx:XML>
then do this
usersXML.users.user[1].firstname
for id
usersXML.users.user[1].#id
Related
I have this xml document in a file called text.xml
The xml document currently looks like this:
<?xml version="1.0" encoding="UTF-8"?>
......
<rss version="2.0"...>
Right after the xml version declaration, I want to add the line:
<?xml-stylesheet type="text/xml" href="style.xslt"?>
In more detail this is what I want to do:
I first want to convert the string contents of the file to an xml document.
$mystr = file_get_contents(path to my text.xml file);
$myxml = new SimpleXmlElement($mystr);
I then want to add an attribute to my XML document to link an XSLT stylesheet.
$myxml->addAttribute(_____);
using the SimpleXmlElement addAttribute method, but the addAttribute method allows for the name, value, and namespace--How would I even use the namespace? Since I have three parameters it makes the most sense for the href to be in the namespace parameter spot.
So I have tried to fill in the blank with 'xml-stylesheet','text/xml','style.xslt' which doesn't work because I am getting the error that "Attribute requires prefix for namespace". How do I get this to work?
Link to php docs: http://php.net/manual/en/simplexmlelement.addattribute.php
Edit: This is different from the other question because I am trying to link an xslt stylesheet to an rss document. Using the "duplicate questions'" solution causes the xslt stylesheet to be linked after the rss version declaration which is not good enough.
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"...>
<?xml-stylesheet type="text/xml" href="test.xslt"?>
....
Instead I want:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xml" href="test.xslt"?>
<rss version="2.0"...>
....
I'm trying to use unset() to delete a node in an XML using PHP and can't figure out what is going on here. It doesn't seem to work correctly and I've seen a lot of other questions of similar nature on here but they don't seem to address this issue directly. Here's what my XML looks like:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<name>Test Name 1</name>
<email>test#test.com</email>
<spouse/>
</user>
<user>
<name>Test Name 2</name>
<email>anotherone#test.com</email>
<spouse>Test Name 3</spouse>
</user>
</data>
My loop that I'm using is like this:
url = 'data/users.xml';
$xml = simplexml_load_file($url);
foreach($xml->user as $theUser){
if($theUser->email[0]=="test#test.com"){
echo "test";
unset($theUser);
}
}
When the e-mail matches "test#test.com" I want to be able to delete that whole user node. It seems that this should work but I can't figure out why it wouldn't? Any help would be greatly appreciated. Thank you!
SimpleXML is fine, no need to switch to DOM, unset() is working fine, if you do it right:
unset($theUser[0]);
see it working: https://eval.in/228773
However there will be a problem with your foreach() if you delete a node mid-loop.
I suggest to use xpath() instead of a loop, IMO elegant and the code is much simpler.
$users = $xml->xpath("/data/user[email='test#test.com']");
will create an array of all <user> with that email-address.
unset($users[0][0]);
will delete the first user in that array.
foreach ($users as $user) unset($user[0]);
will delete the whole array.
see this in action: https://eval.in/228779
SimpeXML is not really meant for changing to the XML structure. Just a simple way of reading the XML.
If you want to manipulate the XML structure you should use the dom functions and more specifically the dom_import_simplexml. This function allows you to import a SimpleXML element and turn it into a DomElement that can be used for manipulation and that includes deletion.
Here is a code sample that solves your problem and demonstrates the usage of dom_import_simplexml.
<?php
$xmlData = '<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<name>Test Name 1</name>
<email>test#test.com</email>
<spouse/>
</user>
<user>
<name>Test Name 2</name>
<email>anotherone#test.com</email>
<spouse>Test Name 3</spouse>
</user>
</data>';
$xml = simplexml_load_string($xmlData);
foreach($xml->user as $theUser){
if($theUser->email == 'test#test.com'){
$dom = dom_import_simplexml($theUser);
$dom->parentNode->removeChild($dom);
}
}
echo $xml->asXml();
When reading this code you might be thinking why this works since we dont save the new structure anywhere after we have executed the removeChild function. This works because the DOM functions does not create copies of the underlying objects but instead manipulates them directly.
Result
<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<name>Test Name 2</name>
<email>anotherone#test.com</email>
<spouse>Test Name 3</spouse>
</user>
</data>
First of all, I just found this site a few days ago and am very happy it exists.
I am facing a problem with updating a child element from a XML Node.
The xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<books>
<book>
<id>1</id>
<bookname>Title 1</bookname>
<bookurl>SomeURLToMyBook</bookurl>
<clicks>0</clicks>
</book>
<book>
<id>2</id>
<bookname>Title 2</bookname>
<bookurl>SomeURLToMyBook</bookurl>
<clicks>0</clicks>
</book>
</books>
I do know how to retrieve the node for book with (e.g.) ID 2 using:
$xml= simplexml_load_file('books.xml);
I then use xpath to find the correct node. as in:
$booknum= $_GET('booklist'); //booklist is the parameter in the querystring
$arrOutput = $xml->xpath("//*[id='".$booknum."']");
What I try to achieve is to update the clicks element and then save the outcome into the existing XML.
I found several code examples on this site but neither one seems to work for me.
I probably do something wrong but I am not really familiar with PHP (learning new things every day!)
So if anybody is willing to help me out I would be grateful.
TIA
I've got the following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<number_of_gr>
3
</number_of_gr>
<group id="0">
<name>Admins</name>
<backend>1</backend>
<every_plugin_feature> 1 </every_plugin_feature>
</group>
<group id="1">
<name>Users</name>
<backend>0</backend>
<every_plugin_feature>0</every_plugin_feature>
</group>
<group id="2">
<name>Moderators</name>
<backend>0</backend>
<every_plugin_feature>0</every_plugin_feature>
</group>
</root>
For Example: I want to delete the group with the id="0". But I don't know how to delete a child with specified attribute in simplexml.
I've tried this code:
<?php
$xml = simplexml_load_file("../xml/groups.xml");
$delgroup = $xml->xpath("/root/group[#id='".$_GET['group']."'");
unset($delgroup);
$xml-> asXML("../xml/groups.xml");
?>
But it doesn't work.
After the process, I'll fill the gap with the id=1, but I can do it without help.
My question is: How to delete the specified group?
You are almost there, just a little tweak:
$delgroup = $xml->xpath("//group[#id='".$_GET['group']."'")[0];
unset($delgroup[0]);
see it working: http://codepad.viper-7.com/ZVXs4O
This requires PHP >= 5.4.
To see a bit of theory behind it: Remove a child with a specific attribute, in SimpleXML for PHP --> see hakre's answer.
PS: Remember to change <number_of_gr> - or delete this node from the XML, because you can always get this number by...
$groups = $xml->xpath("//group");
$numberofgroups = count($groups);
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<firstname>Mark</firstname>
<surname>Zhu</surname>
</user>
</users>
this is the user node I want to add in the front of the existing one
<user>
<firstname>Andy</firstname>
<surname>Li</surname>
</user>
The SimpleXMLElement::addChild can add the in the back of the existing one,
Is there anybody know how to add in the front?
You'll have to use DOM, in particular
DOMNode::insertBefore(DOMNode, DOMNode)
See http://www.php.net/manual/en/domnode.insertbefore.php
What if you read the xml then in a new document, printed your user node + the original xml?
I would recommend to simply add the xml-declaration by hand. You can simply output it before the xml structure.