as3 namespace - get an attribute with a minus sign in it [duplicate] - actionscript-3

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
e4x / as3: How to access a node with a dash in its name.
I've set the namespace for my XML to use SMIL and I'm able to pull the src attribute of an element this way:
my.node.#src which gets "this is some URL"
However, I have another attr called 'system-bitrate'. Because of the minus sign, I can't do #system-bitrate So I attempted what I normally do which is my.node.attribute('system-bitrate') which isn't working.
Oddly enough, not even my.node.attribute('src') works. I suspect this is due to the namespace? So how to I get attributes out using ny.node.attribute ?
The only thing that works is my.node.attributes()[1]. I know that's not the "right way" so I'm hoping someone can enlighten me!
FYI I'm working with SMIL files
** edit **
Here's the namespace required for the XML I'm using:
default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');
And an example of the XML I'm working with:
<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
<head>
<meta name="title" content="Live"/>
</head>
<body>
<switch>
<video src="myStreamName" system-bitrate="200000"/>
</switch>
</body>
</smil>
** code sample for DennisJaaman **
default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');
var xml:XML = XML(event.target.data);
for each(var o:XML in xml.body['switch'].video) {
if(!hs) hs = o;
else {
trace(o.attributes()[1]); // works
trace(o.#url); // doesn't work either (makes me wonder about NS issues
trace(o['#system-bitrate']); // doesn't work
trace(o.attribute('#system-bitrate') // doesn't work
// etc etc, I just left a few in here
}
}

Try to use square brackets like in the sample below:
default xml namespace = new Namespace("http://www.w3.org/2001/SMIL20/Language");
var xmlSmpl:XML=<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
<head>
<meta name="title" content="Live"/>
</head>
<body>
<switch>
<video src="myStreamName" system-bitrate="200000"/>
</switch>
</body>
</smil>;
trace (xmlSmpl.body['switch']['video']['#system-bitrate']);

Behold! The power of QName!
my.node.attribute(
new QName( 'http://www.w3.org/2001/SMIL20/Language', 'system-bitrate' )
)
The thing about attribute (and descendant, and child...) is that its parameter is type * (anonymously). This is because it really isn't a String, it is coerced to a QName (without a URI) in the background. This means you were searching under the default URI for something under the URI above.
Let me know how that code above works out.

Check out this post:
e4x / as3: How to access a node with a dash in its name
******EDIT****:
And use the following notation to get XML Attributes that contain a - (dash)
trace("Video system-bitrate: " + my.node.video["#system-bitrate"]);
These do not work:
trace("Video system-bitrate: " + my.node.video.#["system-bitrate"]);
trace("Video system-bitrate: " + my.node.video.attribute("#system-bitrate"));
For more info check the LiveDocs
Cheers

Related

*ngIf or [hidden] not working correctly

I'm trying to allow for hiding of certain sections of the project I'm working on via user toggle. It saves in the database and gets pulled when the page is loaded in the constructor using the following code
this.http.get(`api/section/get/${this.id}`, this.id).subscribe(res => {
this.section = res.json()[0];
this.sect = res.json();
console.log(this.section);
this.hideIntro = this.sect[0].hideIntro;
this.hideMainVideo = this.sect[0].hideMainVideo;
this.hideHandout = this.sect[0].hideHandout;
this.hideQuiz = this.sect[0].hideQuiz;
console.log("Hide Intro = " + this.hideIntro);
console.log("Hide Main = " + this.hideMainVideo);
console.log("Hide Handout = " + this.hideHandout);
console.log("Hide Quiz = " + this.hideQuiz);
});
The HTML is as follows...
<div class="row classMainBackground col-md-12" *ngIf="!hideIntro">
...content...
</div>
For some reason, no matter what I do, whether I change it to *ngIf="hideIntro == false" or even use [hidden]="hideIntro", it is not working.
Even the console logs in the .ts file show up correctly. Is there a reason why this is not working for me? I've used it in other positions and it works fine there...
Does it have something to do with assigning it in the constructor or something?
Thanks in advance!
Angular change detection runs in response to use interaction with the component. If values are updated outside of that event handling (such as after an HTTP request), you need to manually tell the component that it has changed.
constructor(private changeDetector: ChangeDetectorRef){}
this.http.get(`api/section/get/${this.id}`, this.id).subscribe(res => {
[...]
this.changeDetector.markForCheck();
})
More in depth reading: https://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html
I ended up solving the problem by using {{!section.hideIntro}} in the HTML instead of trying to define a new variable to pass that boolean to.
I believe the answer was a combination of what #Vlad274 and #ConnorsFan were mentioning.
the HTML was returning an [object object] for {{hideIntro}} and it seems like there's a delay between the assigning the new value data from the GET response before the DOM actually loads.
Grabbing the data right from the GET respone variable ended up doing the trick.

Word having single quotes search from xml file using jquery issue

Hi I need to parse XML file using jquery. I created read and display functionality. But when a word having single quote not working.
My XML is like this
<container>
<data name="Google" definition="A search engine"/>
<data name=" Mozilla's " definition="A web browser"/>
</ container>
using my jquery code I can read definition of Google. But I can't read Mozilla's definition due to that single quotes. This is my jquery code.
var displayDefinition = function(obj){
$.get("definitions.xml", function(data){
xml_data1.find("data[name^='"+obj.innerHTML+"']").each(function(k, v){
right=''+ $(this).attr("Defination") + '';
}
}
$(".result").append(right);
}
Any body knows the solution for this please help me.
Thanks
jQuery deals with single quotes very well. the structure of your function looks really wild though. I changed it a big assuming you want to create a function that can display the definition based on passing it a name: http://jsfiddle.net/rkw79/VQxZ2/
function display(id) {
$('container').find('data[name="' +id.trim()+ '"]').each(function() {
var right = $(this).attr("definition");
$(".result").html(right);
});
}
Note, you have to make sure your 'name' attribute does not begin or end with spaces; and just trim the string that the user passes in.

Accessing properties via a String in AS3

I have an engine I created a while back that loads objects into a container based on XML data. A really quick example of the XML would be like this:
<level>
<object cname="enemies.Robot">
<pos x="200" y="400" layer="mobiles" />
</object>
<object cname="Player">
<pos x="12" y="89" layer="mobiles" />
</object>
</level>
I have a class Environment that has a method loadLevel(data:XML) which I parse the XML through, then the function runs through the XML finding all object nodes and uses getDefinitionByName to determine which Object I want to create based on object.#cname.
From here, I have to manually define each property based on the XML like so;
obj.x = xml.pos.#x;
obj.y = xml.pos.#y;
etc.
I was wondering if there's an inbuilt method for setting a property based on a String. By this I mean something like so:
var mc:MovieClip = new MovieClip();
mc.someInbuiltFunctionThatSetsAProperty("alpha", 0.5);
This way I could change my XML to be more like so:
<object cname="Player">
<props>
<x>200</x>
<y>221</y>
<alpha>7834</alpha>
<health>Something</health>
<power>3</power>
</props>
</object>
And iterate through all the children of props to set all of my properties on the fly.
I know if I create an Object and set properties within it like so:
var obj:Object =
{
var1: "hello",
var2: "there",
name: "marty"
};
That you can then iterate through names/values using the for(String in Object) loop like this:
var i:String;
for(i in obj)
{
trace(i + ": " + obj[i]);
}
/**
* Output:
* var1: hello
* var2: there
* name: marty
*/
Is there maybe something even similar to that?
Surely there's a way, as here's an example of identifying a property using a String:
var ar:Array = [new MovieClip(), new MovieClip()];
ar.sortOn("alpha", Array.ASCENDING);
So just to make my question more to-the-point: I want to be able to get and set properties that I can identify using a String.
Why not using ["string property"] notation :
var mc:MovieClip=new MovieClip()
mc["alpha"] = 0.5 // setter
var alpha:Number=mc["alpha"] // getter
I'm not quite clear on what it is you're looking for exactly, but I have a general sense of what you're getting at and have a few suggestions for you. First, have a look at the documentation for the Object class in the AS3 Language Reference. Look specifically at the propertyIsEnumerable() and setPropertyIsEnumerable() methods. I think that's what you're asking about.
If not, you might want to look into the behavior of dynamic classes, which let you add variables to an object on the fly.

jsf messages: adding link

Currently in JSF, all HTML contained within a message (rich:messages tag) is escaped and just shows up as the markup. For example, in my backing bean, I have:
createMessage("Title created successfully with product number: " + product.getProductNumber() + ".");
where createMessage() is just a helper function that adds a new Message to the faces context and is then viewable in my rich:messages tag.
When this message is created, my message simply shows up with the escaped HTML:
Title created successfully with product number: 1234.
Is there any way to avoid this and just provide an actual link in the message instead?
Thanks in advance
~Zack
A quick solution is to create a new renderer.
I've done this for h:messages as I wanted to separate the messages of different severities into separate divs. If you never want to use the default renderer then it's a good option.
The standard class that you would overwrite/extend is:
public class MessagesRenderer extends HtmlBasicRenderer
You would just use a ResponseWriter that doesn't escape the text. The concrete class is the HtmlResponseWriter which escapes the text. You could extend this and overwrite the
public void writeText(Object text, String componentPropertyName)
so that it doesn't use HtmlUtils.
Then just add your new renderer to faces-config.xml
<render-kit>
<renderer>
<component-family>javax.faces.Messages</component-family>
<renderer-type>javax.faces.Messages</renderer-type>
<renderer-class>com.mypackage.MessagesRenderer</renderer-class>
</renderer>
</render-kit>
It sounds like you need to create your own version of rich:messages that has an escape attribute, like h:outputText, so you can disable HTML escaping.
If you're using jquery you can unescape the xml characters:
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
$(".esc").each(function(i) {
var h = $(this).html();
h = h.replace(/</gi, "<");
h = h.replace(/>/gi, ">");
$(this).html(h);
});
});
//]]>
</script>

Sending values through links

Here is the situation: I have 2 pages.
What I want is to have a number of text links(<a href="">) on page 1 all directing to page 2, but I want each link to send a different value.
On page 2 I want to show that value like this:
Hello you clicked {value}
Another point to take into account is that I can't use any php in this situation, just html.
Can you use any scripting? Something like Javascript. If you can, then pass the values along in the query string (just add a "?ValueName=Value") to the end of your links. Then on the target page retrieve the query string value. The following site shows how to parse it out: Parsing the Query String.
Here's the Javascript code you would need:
var qs = new Querystring();
var v1 = qs.get("ValueName")
From there you should be able to work with the passed value.
Javascript can get it. Say, you're trying to get the querystring value from this url: http://foo.com/default.html?foo=bar
var tabvalue = getQueryVariable("foo");
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++)
{
var pair = vars[i].split("=");
if (pair[0] == variable)
{
return pair[1];
}
}
}
** Not 100% certain if my JS code here is correct, as I didn't test it.
You might be able to accomplish this using HTML Anchors.
http://www.w3schools.com/HTML/html_links.asp
Append your data to the HREF tag of your links ad use javascript on second page to parse the URL and display wathever you want
http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript
It's not clean, but it should work.
Use document.location.search and split()
http://www.example.com/example.html?argument=value
var queryString = document.location.search();
var parts = queryString.split('=');
document.write(parts[0]); // The argument name
document.write(parts[1]); // The value
Hope it helps
Well this is pretty basic with javascript, but if you want more of this and more advanced stuff you should really look into php for instance. Using php it's easy to get variables from one page to another, here's an example:
the url:
localhost/index.php?myvar=Hello World
You can then access myvar in index.php using this bit of code:
$myvar =$_GET['myvar'];
Ok thanks for all your replies, i'll take a look if i can find a way to use the scripts.
It's really annoying since i have to work around a CMS, because in the CMS, all pages are created with a Wysiwyg editor which tend to filter out unrecognized tags/scripts.
Edit: Ok it seems that the damn wysiwyg editor only recognizes html tags... (as expected)
Using php
<?
$passthis = "See you on the other side";
echo '<form action="whereyouwantittogo.php" target="_blank" method="post">'.
'<input type="text" name="passthis1" value="'.
$passthis .' " /> '.
'<button type="Submit" value="Submit" >Submit</button>'.
'</form>';
?>
The script for the page you would like to pass the info to:
<?
$thispassed = $_POST['passthis1'];
echo '<textarea>'. $thispassed .'</textarea>';
echo $thispassed;
?>
Use this two codes on seperate pages with the latter at whereyouwantittogo.php and you should be in business.