How to change the nodeType of XML document from DOCUMENT to ELEMENT - xmldom

I used XMLDOM to create a document (#1). I used Load("string"). With another XML document (#2), I want to append the first XMLDOM, but I get an error stating "This operation can not be performed with a Node of type DOCUMENT." How can I change the node to type ELEMENT (1)?
oDOM2 = Createobject(MSXML2.DOMDocument)
<bunch of code and other things go here...>
oDOM1 = Createobject(MSXML2.DOMDocument)
oDOM1.Load("<SomeXML><MoreXML></MoreXML></SomeXML>")
oDOM2.appendChild(oDOM1) -->Error
If i use the DOM object to create the object with createElement and addChild, with that fix the problem?

I figured out one way to handle this. After I have completed my document #1, then I can select a single node (the root) into a new DOM object. It works for my purpose.
oDOM2 = Createobject(MSXML2.DOMDocument)
<bunch of code and other things go here...>
oDOM1 = Createobject(MSXML2.DOMDocument)
oDOM1.Load("<SomeXML><MoreXML></MoreXML></SomeXML>")
oDOMTemp = oDOM1.selectSingleNode("//SomeXML")
oDOM2.appendChild(oDOMTemp)
Is there a better way?

Related

Selenium not giving whats inside a class?

PATH = "D:\CDriver\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://www.se.com/ww/en/about-us/careers/job-details/inside-sales-associate/006ZMV')
TITLE = driver.find_element_by_class_name('sdl-application-job-details__job-title')
print(TITLE)
driver.quit()
I have all the needed imports, I just wanted to leave them out.
When I run this the output SHOULD be: Inside Sales Associate
But instead it gives me this: <selenium.webdriver.remote.webelement.WebElement, the session and element code.
What do I need to do to make it print what it should print. I have tried by_tag_name('h1.sdl-application-job-details__job-title') but that gives the exact same.
There is a inbuilt title method available in Selenium. You can call that method on driver object not on web element.
Code :
driver.get('https://www.se.com/ww/en/about-us/careers/job-details/inside-sales-associate/006ZMV')
driver.title
print(driver.title)
or if you want to retrieve text inside any web element, you could probably do something like this :
class_value = driver.find_element(By.CSS_SELECTOR, "h1[class$='sdl-application-job-details__job-title']").text
print(class_value)
The find_element methods return web elements. Just pass print(TITLE.text)

Automatically change the type of the elements in an array

I wrote a class for my project like this using typescript and react.
class myImage extends Image {
oriHeight: number;
}
After I uploaded two images I have an array named 'results' which is full of objects with type myImage.
[myImage, myImage]
When I click it in browser, I could see the data of oriHeight of each element.
Then I try to use results.map() method to traverse all the elements in that array.
results.map((result: myImage) => {
console.log(result);
var tmp = result.oriHeight;
console.log(tmp);
})
However, the output of result is no longer an object but an img tag (because the type of Image is a HTMLElement) which makes the data of result unreadable. So the output of every tmp is undefined.
I am confused about that. Why the myImage object will become an img tag when I want to traverse it? I hope someone could help me with that. Really appreciate it.
I bet your data is actually fine. When you console log an html element, the chrome console displays it as an html tag instead of the javascript object.
Update: It's generally a bad practice to add your own properties to DOM elements because they're harder to debug and you risk them being overwritten by future browser properties. Instead, you could create a javascript object that contains both the image and your custom property. Here's an example interface definition:
interface MyImage {
imageEl: HTMLImageElement;
oriHeight: number;
}

CodeMirror - what is addWidget for and how to use it?

I want to make some extensions to CodeMirror. The addWidget method seems like a promising starting point. The documentation states
addWidget(pos, node, scrollIntoView) Puts node, which should be an
absolutely positioned DOM node, into the editor, positioned right
below the given {line, ch} position. When scrollIntoView is true, the
editor will ensure that the entire node is visible (if possible). To
remove the widget again, simply use DOM methods (move it somewhere
else, or call removeChild on its parent).
I don't really understand what that means or what I would use it for. I cannot find a usage of it in the CodeMirror codebase nor anywhere else in google.
You need to pass an html node and a position and a Boolean value
// create a node
var htmlNode =document.createElement("h1");
var text = document.createTextNode("Text or whatever");
htmlNode.appendChild(text)
// call this after you initialized the editor.
// the position must be like this {ch: YourCharecterNumber, line: YourLineNumber}
editor.addWidget({ch:30 , line: 1},htmlNode, true)

using innerHTML at custom tag in IE

I have a problem I just can't solve, and need your advice since I'm out of ideas:
Context: I'm using tinyMCE Editor on my website and developed a custom plugin to include external xml files. So far everything works as expected. The links to the external xml files are represented as span-Tags:
<span id="-[XML Document 1]-" title="erg" class="xml_embed xml_include">-[XML Document 1]-</span>
but only in the tinyMCE editor with a custom class (xml_include) to distinguish them from normal text and upon switching to the html/source code view or saving, those span tags get replaced to xi:include elements:
<xi:include xmlns:xi="http://www.w3.org/TR/XInclude" show="xml_embed" href="erg">-[XML Document 1]-</xi:include>
The text that was set as innerHTML ("-XML Document 1]-") for the span tag(s) serves as placeholder in the editor and gets moved to the xi:include tag(s) in the source view and serves as placeholder there also.
Now to the problem:
The code to transform span.xml_include to xi:include gets called before the source code popup is displayed:
ed.onPreProcess.add(function(ed, o) {
var elm;
var domelm;
//get all span.xml_include elements
tinymce.each(ed.dom.select('span.xml_include', o.node), function(n) {
//IE ignores innerHTML when created with tinymce.dom, therefore use native JS createElement method to tell IE that custom tag is valid HTML
if(tinymce.isIE)
{
domelm = document.createElement('xi:include');
domelm.setAttribute("xmlns:xi", "http://www.w3.org/TR/XInclude");
domelm.href = n.title;
domelm.innerHTML = n.innerHTML;
domelm.show = n.className.split(/\s+/)[0];
document.body.appendChild(domelm);
ed.dom.replace(domelm, n);
}
else
{
//ed = tinyMCE.activeEditor
elm = ed.dom.create('xi:include', {href: n.title, show: n.className.split(/\s+/)[0]}, n.innerHTML);
elm.setAttribute("xmlns:xi", "http://www.w3.org/TR/XInclude");
ed.dom.replace(elm, n);
}
});
});
this code works perfectly fine in FF and Chrome, but not in IE (I tested 7 & 8): in IE the innerHTML of the new element "domelm" can't be set. Either it stays blank or if set explicitly an error is thrown. n.innerHTML can be accessed. I get an "Unknown runtime error" for the line domelm.innerHTML = n.innerHTML;
What else did I try?
the native JS way: domelm.appendChild(document.createTextNode(n.innerHTML)); to create a text node and append it to the "domelm" with no success (getting error: "unexpected call to method or property access", that should be the translation from "Unerwarteter Aufruf oder Zugriff" (german version))
the tinyMCE API way: tinymce.DOM.setHTML(domelm, n.innerHTML); resulted in no error but also the usual blank innerHTML
the jQuery way: $('#domelm').html(n.innerHTML); or first var jQelm = $(domelm); then jQelm.text(...); or jQelm.html(...); doesn't matter, neither works, IE always returns "unexpected call to method" error in the jquery core, which I obviously won't touch..
the tinyMCE way of creating elements as seen in the "else" part of the if condition above..if domelm.innerHTML = n.innerHTML; isn't explicitly set, elm.innerHTML just stays blank, else the same errors as on the approaches above occur, therefore I could as well skip the if(tinymce.isIE) detection..
What else can I do? Suggestions?
I also made sure to properly declare the custom xml namespaces, changed the MIME-type to application/xhtml+xml instead of simply text/html, "announced" the xi:include node for IE with document.createElement('xi:include'); and generally changed the code to please IE..and this seems to be the last major bug I have to overcome..
I'm not sure if it's an error in my code since FF and Chrome work fine local and remote and don't show any errors..?
Any help is appreciated, I hope I provided enough context for you so that it's clear what I meant. and sorry for mistakes, English is not my first language :)
Ok, wrapping the custom element in a p/div/span tag finally did the trick: I used span to leave the formatting unmodified..here is what I did:
In the "if(tinymce.isIE) part of the onPreProcess function, before "xi:include" is created, a wrapper is needed:
var wrapper = document.createElement('span');
Appending the custom tag-element to the wrapper:
wrapper.appendChild(domelm);
and appending a textNode to the wrapper since appending it to the domelm throws errors:
wrapper.appendChild(document.createTextNode(n.innerHTML));
and finally append the wrapper to the dom and replace the "span" tag (n) with the wrapped "xi:include" (wrapper, span tag to be modified):
document.body.appendChild(wrapper);
ed.dom.replace(wrapper, n);`
The result is a custom "xi:include" tag in IE with the correct innerHTML:
<span><xi:include xmlns:xi="http://www.w3.org/TR/XInclude" href="eh" show="xml_embed">-[XML Document]-</xi:include></span>

Does JSON.js cause conflicts with Sys.Serialization.JavaScriptSerializer.serialize

I am using Telerik controls in my webforms and want to serialize object on the client. Since I am getting a stackoverflow error with Sys.Serialization.JavaScriptSerializer.deserialize I wanted to try JSON. With both JSON and and the MS library I get "Sys.Application is undefined."
Has anyone encountered this what did you do as a work around?
EDIT
I am serializing my object on a parent page and passing them via an argument to a child window. The child window is in an IFRAME tag. The object can be used in the child page, but I receive the stackoverflow error when I serialize it. The object is an Array of objects.
I may have a work around. On the parent page I do the following:
// Call Dialog Window
var radWindow = window.radopen(null, "DialogWindow");
var args = new Object();
// Assign DisplayValues
args.serialPairs = Sys.Serialization.JavaScriptSerializer.serialize(jsonDataValuePairs);
radWindow.argument = args;
radWindow.SetUrl("AssignCCMProfile.aspx?CCMId=" + ccmId + "&ExecDate=" + execDate);
radWindow.Center();
radWindow.Show();
On the child page I do the following:
dataValuePairs = Sys.Serialization.JavaScriptSerializer.deserialize(args.serialPairs);
This seems like a kluge to me. It works, but I'd rather pass the object from the parent window to the child window and still be able to serialize the object on the child window.