Set new Attribute in XML using jQuery - php

i want to add new attribute in XML using jquery
for e.g:
XML:
new XML i want display as:
<data>
<section marked="marked"></section>
</data>
from above i want to add marked="marked" attribute using jquery.

You can add attribute in any XML tag as you add in HTML
First your XML is saved in any variable
var a = '<data><section></section></data>';
Edit
Now you have to add this line too and it will work fine.
a = $(a);
This line will add an attribute in section tag
a.find('section').attr('marked', 'marked');

Related

php xml DOMDocument close tag element

I am using PHP DOMDocument() to generate XML file with elements.
I am appending all details into sample xml file into components tag. But closing tag is not coming. I want to create closing tag.
My Code is doing this
<component expiresOn="2022-12-31" id="pam" />
I want to do like following
<component expiresOn="2022-12-31" id="pam"></component>
My PHP CODE SAMPLE
$dom = new DOMDocument();
$dom->load("Config.xml");
$components = $dom->getElementsByTagName('components')->item(0);
if(!empty($_POST["pam"])) {
$pam = $_POST["pam"];
$component = $dom->createElement('component');
$component->setAttribute('expiresOn', $expirydate);
$component->setAttribute('id', "pam");
$components->appendChild($component5);
}
$dom->save("Config.xml");
I tested following suggestion and its not working. Both xml-php code are different.
$dom->saveXml($dom,LIBXML_NOEMPTYTAG);
Self-closing tags using createElement
I tested following.
You're trying to use DOMDocument::saveXML to save the new XML back into the original file, but all that function does is return the XML as a string. Since you aren't assigning the result to anything, nothing happens.
If you want to save the XML back to your file, as well as avoiding self-closing tags, you'll need to use the save method as you originally were, and also pass the option:
$dom->save('licenceConfig.xml', LIBXML_NOEMPTYTAG);
See https://3v4l.org/e6N5s for a demo

Get information from a form and writing to an xml file using php?

I want to get information from a form and writing to an xml file by using php ? My xml file is in tree structure
Example of my xml file:
<BookStore>
<Book>
<name>TODO</name>
<url>TODO</url>
</Book>
</BookStore>
when I submit it will create a xml but I want Book and BookStore also as a parent. I created till name and url. But I dont know how to create Book and BookStore too?
You just need to create new parent elements and then append the child.
$bookstore = $doc->createElement('Bookstore');
$book = $doc->createElement("Book");
/* your code */
/* append name and url to $book */
$book->appendChild($textareaNode);
$book->appendChild($textareaNode1);
$bookstore->appendChild($book);
$doc->appendChild($bookstore);
$doc->save("d.xml");

simplexml add child non attribute information

i'm trying to figure out how to add this right here;
<tech name="level 0" prereq="0" id="7" null="null">
<meta fuelx="1" energyx="1" cost="100" income="11">Description goes here</meta>
</tech>
to an already existing xml file.
I've gotten the first and the but I cannot figure out how to add the information between the &.
You can't add child nodes w/ SimpleXML. You can convert to DOM and then do it though.
$curNode = dom_import_simplexml($simpleXml);
$newChild = dom_import_simplexml($sXml);
$tmpNode = $curNode->ownerDocument->importNode($newChild, true);
$curNode->appendChild($tmpNode);
EDIT
Wow, it seems like they added an addChild method in 5.1.3, where have I been??

PHP XML DOM getElementById

I need to pick one tag from xml file and insert another tag before this tag. I'm doing this with method insertBefore in DOM, but the problem is, that if I want pick the tag before I want to add the another tag by method getElementById, it doesn't work.
It writes "using non-object". This is how the tag looks:
<item id="Flow_0" href="Flow_0.html" media-type="application/xhtml+xml"/>
Somewhere I read that it must look like this, but I can't edit all files:
<item xml:id="Flow_0" href="Flow_0.html" media-type="application/xhtml+xml"/>
Have you got any idea how to do that?
A common workaround is to use XPath to get the element.
$item = $xpath->query('//item[#id="Flow_0"]')->item(0);

Output HTML in XML Attribute

I am attempting to display HTML information in an attribute of a XML document. For example, I somehow want to accomplish the following:
Attributes:
id = "id"
HTML_Elements = {
link
}
(<something id='id' HTML_Elements='code'></something>)
I am wondering if there is some way to accomplish displaying the code directly inside of the attribute without having to link to it on another page.
Thanks for any help!
XML attributes may not contain HTML.
See Chapter 3.3.3 Attribute Value Normalization in the XML 1.0 Specs

Categories