Flex String to HTML for parse with DOM - actionscript-3

I have a string:
private str:String = '
<!DOCTYPE html>
<html>
<body>
<div>TEST</div>
<span id="t1">T1 Content</span>
<span class="t2">T2 Content</span>
</body>
</html>';
I want to parse the string. Next, I get innerHTMl via Object, ID or Class.
E.g.:
Objects: Body, Div, Span
IDs: t1
Classes: t2
In PHP with the class, it's easy. But I could not create this build with Flex.
Thanks for help...

Convert it to native XML in as3, then use e4x to parse the information required:
var xml:XML = XML(str);
//trace the content of any span with the id of "t1"
trace(xms..span.(attribute("id") == "t1"));
//trace the content of any span with the class t2
trace(xml..span.(attribute("class") == "t2"));
//trace the contents of the first div
trace(xml..div[0]);
For more on e4x, this is a good primer:
http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4

Related

I want to make the object and bind it into html in typescript

I want to make the object and bind it into html file in typescript. This code is in javascript format so what should i do to make an object and bind it in html file.
<map>
<div>
<div #map style="position:relative;width:100%;min-width:290px;height:600px;"></div>
</div>
<div>
<button (click)="searchAddress()">Search</button>
</div>
</map>
showPopup(e): void {
//Get the properties and coordinates of the first shape that the event occurred on.
var p = e.shapes[0].getProperties();
var position = e.shapes[0].getCoordinates();
//Create HTML from properties of the selected result.
var html = `
<div style="padding:5px">
<div><b>${p.poi.name}</b></div>
<div>${p.address.freeformAddress}</div>
<div>${position[1]}, ${position[0]}</div>
</div>`;
//Update the content and position of the popup.
this.popup.setPopupOptions({
content: html,
position: position
});.

Selenium test (selection of element having no attribute)

<! DOCTYPE html>
<html>
<head>Sample</head>
<body>
<div class="panelBody">
<div class=panel-section></div>
<div class=panel-section style="display:block"></div>
</div>
</body>
</html>
In given Snippet there are two elements with same class. I have to select the element which does not having style attribute.If i tried to search with panel-section class its giving ambiguity error.So how to select div element which does not having style attribute.i.e
<div class=panel-section></div>
Try this:
//div[#class='panelBody']/div[not(#style)]
Explanation: First find the div with class panelBody, then find child div elements in the panelBody div which doesn't contain #style attribute.
Use findElements method if there are more than one div element without #style attribute, otherwise findElement() method would suffice.
Since there are more than one elements with same class name, you need to use Selenium's driver.findElements() method. I have tried getting this element, but I wonder if it is clickable. Only element can actually be useful here is text Sample.
Check below code. Let me know if it is similar to what you are looking for.
List<WebElement> linksize=null;
String links[]=null;
linksize = driver.findElements(By.cssSelector("div[class=panel-section]"));
int linksCount = linksize.size();
links= new String[linksCount];
for(int i=0;i<linksCount;i++)
{
links[i] = linksize.get(i).getAttribute("style");
if(links[i].isEmpty())
{
System.out.println("I am div without style");
linksize.get(i).click();
}
}

remove parent node while preserving child node in jsoup

Consider this sample code-
<div>
<outer-tag> Some Text <inner-tag> Some more text</inner-tag></outer-tag>
</div>
I want to get the following output -
<div>
<inner-tag> Some more text</inner-tag>
</div>
How do I achieve this? Thanks!
This solution will work for your current example:
String html = "<div>"
+ "<outer-tag> Some Text <inner-tag> Some more text</inner-tag></outer-tag>"
+ "</div>";
Document doc = Jsoup.parseBodyFragment(html);
for (Element _div : doc.select("div")) {
// get the unwanted outer-tag
Element outerTag = _div.select("outer-tag").first();
// delete any TextNodes that are within outer-tag
for (Node child : outerTag.childNodes()) {
if (child instanceof TextNode) child.remove();
}
// unwrap to remove outer-tag and move inner-tag to child of parent div
outerTag.unwrap();
// print the result
System.out.println(_div);
}
Result is:
<div>
<inner-tag>
Some more text
</inner-tag>
</div>

JSoup Add Wrapper div after body

I am trying to add a <div class="wrapper"> to my generated html after the body tag. I want the ending </div> to be before the ending </body>. So far I have
private String addWrapper(String html) {
Document doc = Jsoup.parse(html);
Element e = doc.select("body").first().appendElement("div");
e.attr("class", "wrapper");
return doc.toString();
}
and I am getting
</head>
<body>
</head>
<p>Heyo</p>
<div class="wrapper"></div>
</body>
</html>
I also can't figure out why I am getting "</head>" in the html too. I only get it when I use JSoup.
Jsoup Document normalizes the text with normalise method. The method is here in Document class. So It wraps with and tags.
In Jsoup.parse() method it can take three parameter, parse(String html, String baseUri, Parser parser);
We will give the parser parameter as Parser.xmlParser which is using XMLTreeBuilder (Otherwise it uses HtmlTreeBuilder and it normalises html.).
I tried, latest code (it may be optimize) :
String html = "<body></head><p>Heyo</p></body>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
Attributes attributes = new Attributes();
attributes.put("class","wrapper");
Element e = new Element(Tag.valueOf("div"), "", attributes);
e.html(doc.select("body").html());
doc.select("body").html(e.toString());
System.out.println(doc.toString());

Searching in html on the behalf of ID

Is searching possible in html tags on the behalf of ID? for example to find div tag having id="abc".
I can use document.getElementByID("abc"). But i need parent div + its inner HTML in return of searching. i.e if this div has childs
Try this :-
<script >
function showHTML(){
var vinner=document.getElementByID("abc").innerHTML;
var totalinner="<div >"+vinner+"</div>";
alert(totalinner);
}
</script>
HTML part:-
<body onload="showHTML();">
<div id="abc">
Hello inside abc
<div>
Inner div inside abc tag.
</div>
</div>
</body>
Its working fine. You can get Attributes here.
It's hard to understand what you want to achieve:
document.getElementById("abc").parentNode.innerHTML;
//will return <div id="abc"> and other items from parrent
document.getElementById("abc").getAttribute("name");
//will atribute of <div id="abc">
if (document.getElementById("abc").hasChildNodes()) {
// It has at least one
}
Using jQuery is much simplier, you could do that:
$("#abc").attr('id') //retunrs id
$("#abc").attr('class') //returns classes
//or other manipulations
One way to do this is to use outerHTML, which:
gets the serialized HTML fragment describing the element including its descendants.
Given the following HTML:
<div id="abc" data-attr="A custom data-* attribute">Some text in the div.</div>
The following JavaScript will log, in the console, the HTML of the element of id equal to abc:
var htmlString = document.getElementById('abc').outerHTML;
console.log(htmlString);
JS Fiddle demo.
References:
outerHTML.
outerHTML compatibility.