I'm looking to display some XML data but am having issues.
The xml data I'm looking to display is listed below:
<?xml version="1.0" encoding="utf-8"?>
<rooms>
<not_in_rooms>
</not_in_rooms>
<room name="Room1" description="text" users="0" id="r12" passworded="false" owner_username="admin" owner_siteid="" >
</room>
<room name="Room2" description="text" users="0" id="r13" passworded="false" owner_username="admin" owner_siteid="" >
<user ip="0.0.0.0" name="user1" siteId="" gender="male" cam="0" camIsPrivate="false" mic="0" >
</user>
</room>
I want to display the Room Names (i.e. Room1, Room2) and the users in each room (i.e. user1). I found generic xml to html tutorials but they don't explain what to do when there are attributes called within each element.
You could use several methods, based on what you want. Probably the easiest is XSLT or parsing it with a server-side scripting language like PHP.
XSLT is probably the closest to the "standard" way to do this. You just have to link to/embed the XML file and add an element to the file stating that you should use an XSLT stylesheet. There are plenty of XSLT tutorials online. If you are wondering about how to read all the attributes, see this question.
You could also use PHP to parse and output the HTML. This would be much more complicated but also more flexible.
You can use pure JavaScript and DOM parser to parse your XML.
Assume you have your XML as a variable in JavaScript:
var txt = "<XML></XML>"
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(txt);
}
//get take data..
xmlDoc.getElementsByTagName("Yourtagname")[0]
//Get Attribute Data...
xmlDoc.getElementsByTagName("Yourtagname")[0].getAttribute('YourAttributeInTag')
Here is a test fiddle from friends to try out.
http://jsfiddle.net/D2QpZ/
Related
For a personal project I need to get information from an excel-sheet into xml-data (xml-schema needed) but if I try to validate it this error shows: "Non-HTML Content-Type: text/xml ."
The excel sheet includes information such as basic text, which is quite simple to transport into xml, but also has long html inside its cells which later should be used as content of a wordpress site/post. So this hmtl begins with visualcomposer elemnts like "[vc_row][vc_column][vc_column_text]" and further more contains very standard html elements like paragraphs or tables. All html is wrapped inside .
So I wonder if this could be the source of my problem as, with my very slim coding and debugging knowledge, this came in my mind first.
Some extra information:
the xml-schema is edited with Dreamweaver CC 2015 which also does the validation
the xml-file is characterized, with: <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<materialData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Data inside the xml is structured like: <matData><ID>3001</ID><author>1</author> ... </matData>
I suspect what you need is to correctly enclose the values that will include html in cdata tags so there not seen as xml.
<exampleOfACDATA>
<![CDATA[
Since this is a CDATA section
I can use all sorts of reserved characters
like > < " and &
or write things like
<foo></bar>
but my document is still well formed!
]]>
</exampleOfACDATA>
see further details here
What does <![CDATA[]]> in XML mean?
I've an XML file generated as an output of a Java program. This contains some text in the form of html in which the tags are written with < and > instead of < and > respectively. I want to convert this xml to html where in the inner html is also processed. For example:
My xml snippet:
<company>
<companyEnhancement>
Rank: -1</br> Other Links</br>http://www.gehealthcare.com/</br>
</companyEnhancement>
</company>
And, my xslt stylesheet has this part to parse it:
<td>
<xsl:value-of select="companyEnhancement"/>
</td>
But the html output on the browser, has this data as it is within a table cell:
Rank: -1 </br> Other Links</br>http://www.gehealthcare.com/</br>
I read through the links here, but I am not able to understand what exactly I should do in the stylesheet.
Please help me out. I am very new to xslt, so please excuse if it is a silly question.
Thanks
You say you have HTML in there but </br> is not HTML syntax, that would be <br> or perhaps for XHTML <br />.
If you really have escaped HTML and want to output that then you can try
<xsl:value-of select="companyEnhancement" disable-output-escaping="yes"/>
but that is only going to help if your XSLT processor serializes the result tree. Firefox for instance does not do that and does not support disable-output-escaping.
It is possible with an xml parser to extract an html text?
Explaining in detail:
I have this simple xml
<?xml version="1.0" encoding="iso-8859-1"?>
<eventi>
<evento><id_evento>4553</id_evento><descrizione>Lorem Ipsum<a href='http://www.yea.it/yea.asp' target='_blank'><span class='U'>Vai alla pagina di gioco</span></a></descrizione></evento>
</eventi>
and i'm parsing it with rapture XML, while developing an app for IOS. When i do
rootXML = [RXMLElement elementFromURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#", indXMLdettaglioEvento, idElemento]]];
[rootXML iterateWithRootXPath:#"//evento" usingBlock: ^(RXMLElement *datiXML) {
NSLog(#"%#",[datiXML child:#"descrizione"].text);;
]}
The nslog of [datiXML child:#"descrizione"].text returns the text without the html tags. It is possible to make it return the entire html?
[datiXML child:#"descrizione"]
returns a parsed XML item which has a text of Lorem Ipsum, but it also has children itself! The first child, I think you find, is going to be an XML item for your link anchor:
[[dataiXML child::#"descrizione"] child: #"a"] => XML item for link
[[[dataiXML child::#"descrizione"] child: #"a"] child: #"span"] => XML item for span
So you'll need to traverse the whole tree to parse your xHTML -- but I think you'll find it's all there.
As previous commenters have said, lots of valid HTML pages are not valid XML. And lots of HTML pages that "work" aren't valid! So this wouldn't be a good strategy for writing a Web browser. But that's not what we're doing here; if the service you're talking to delivers XML, it makes perfect sense to use an XML parser to parse it!
You can use some open source libraries like TinyXML, TouchXML etc for parsing XML documents.
Otherwise you can write your own parser using NSXMLParser.
Hope this Helps !!!
So from what I understand of the CDATA tags on an XML document, it's meant to allow HTML tags inside it to function as they normally would.
I am working with a website template here that uses XML files, and while editing one of the documents to add a href or image to it, I rather get a string. Here's the code, if you can help me that would be greatly appreciated.
I have tried modifying the code to use the < / > that it seems to want to suggest in the output text, but that doesn't help either. If anybody can explain or link me to relevant information, or both, it would be amazing =)
<?xml version="1.0"?>
<xmldata section="Address">
<content>
<image src="resources/images/streetmap1.png" />
<bodytext><![CDATA[Shop 6, 106 Foster Street
Dandenong, 3175
Street Map:
<image src="resources/images/streetmap1.png" />
]]></bodytext>
</content>
</xmldata>
EDIT - Supplying accompanying .js file.
http://pastebin.com/Td2EYiKH
So from what I understand of the CDATA tags on an XML document, it's meant to allow HTML tags inside it to function as they normally would.
No. It is so that characters with special meaning in XML (such as <) can be included as data without escaping.
Here's the code
You need to look at the code that transforms the XML data into the HTML content. You haven't shared that with us.
<image src="resources/images/streetmap1.png" />
The HTML element is img not image and it should have an alt attribute.
I'm completely new to using XSL, so if there's any information that I'm neglecting to include, just let me know.
I have a string in my XSLT file that I can display like this:
<xsl:value-of select="#Description/>
and it shows up, rendered in a browser like:
<div>I can't do anything about the html entities existing in the text.</div> <div>This includes quotes, like "Hello World" and sometimes whitespaces. </div>
What can I do to get this string rendered as html, so that <div></div> results in newlines, " gives me ", and gives me a space?
I could elaborate on things I've already tried that haven't worked, but I don't know if that's relevant.
I think you want to set the following attribute as so:
<xsl:value-of select="#Description" disable-output-escaping="yes"/>
Why do you need to have entities output? To the browser is the same as -- in both cases it will display a non-breaking space.
There is a feature in XSLT 2.0 called character-maps, that provide this functionality, if really needed. It is an XSLT best practice to try not to use DOE, unless absolutely necessary.
Also, DOE is not a mandatory feature of XSLT and some XSLT processors may not implement it. This means that an XSLT application that uses DOE is generally not portable across different XSLT processors.
The reason divs in HTML get an endline is completely different and related to the CSS boxmodel. Most browsers apply the style:
div {display:block;}
In lieu of the standard display:inline;. However, they only do that to divs in the XHTML namespace. You need to output divs to the XHTML namespace to faciliate that. Bind the XHTML namespace to the prefix xhtml at the top of your document like so:
<xsl:stylesheet xmnls:xhtml="http://www.w3.org/1999/xhtml" ... >
And then output the divs as <xhtml:div> ... </xhtml:div> most browsers would recognise the div to be in the XHTML namespace (http://www.w3.org/1999/xhtml) and apply the block style.