How to determine in an XML node exists in Coldfusion
If I have XML that looks something like this:
<manifest>
<state ID="20" name="State 2">
<person>john.black</person>
</state>
<state ID="30" name="State 2">
<person>bill.doe</person>
<group>Group2</group>
</state>
</manifest>
Where <group> may or may not exist under <state>, how do I determine if <group> exists when I'm parsing this XML?
This is the code I'm working with currently:
<cfset szXMLFile = "Manifest.xml">
<cfset XMLDoc = xmlParse(szXMLFile)>
<cfset StateNodes = xmlSearch(XMLDoc,'/manifest/state')>
<cfoutput>
<cfloop from="1" to="#ArrayLen(StateNodes)#" index="i">
<cfset StateXML = xmlparse(StateNodes[i])>
<cfloop index="p" from="1" to="#ArrayLen(StateXML.state.person)#">
<cfoutput>Person: #StateXML.state.person[p].xmltext#</cfoutput><br />
</cfloop>
<cfloop index="g" from="1" to="#ArrayLen(StateXML.state.group)#">
<cfoutput>Person: #StateXML.state.group[g].xmltext#</cfoutput><br />
</cfloop>
</cfloop>
</cfoutput>
I suppose I could just put <try><catch> blocks around where I'm referencing <group> and if it doesn't exist, I'll just handle it that way - but that seems like such a hack. Is there a 'real' way to determine if the node actually exists?
Thanks!
<cfif structKeyExists(StateXML.state, "group")>
<cfloop index="g" from="1" to="#ArrayLen(StateXML.state.group)#">
<cfoutput>Person: #StateXML.state.group[g].xmltext#</cfoutput><br />
</cfloop>
</cfif>
Similar Messages
-
How to identify if a data node exist?
Hi,
If a form is binded to a schema, and when the xml is supplied to the form how to identify an optional node in the xml is available or not.
for example if I have a XML input like below
<root>
<childTable>
<childRow>
<child1>
<child2>
</childRow>
<childRow>
<child1>
<child2>
</childRow>
</childTable>
</root>
is it possible to identify if the childTable node is present in the xml input.
The code xfa.datasets.data.root.childTable if used fails when the childTable is not present in the input xml?
Please let me know if there is a way to identify if the input xml has a specified node. Assuming that we know the path of the node in the xml structure.
Regards -
Ashok DeivaisgamaniHi Ashok,
Could you please try this?
var nodeCheck = xfa.resolveNode("$record.childTable");
if(nodeCheck != null)
app.alert("Node Exists");
else
app.alert("Node Not Avaliable");
Thanks,
VJ -
How to find in a xml node by its localName ?
i have an a xml with namespace
but i dont know this namespace and how many its in this xml
so i know tah will be a tag named hash
i want to get the actual value
and assign a new value
thanksHi,
With debug mode using breakpoints you should be able to stop the app at the time you data is coming back and see the structure of data result.
Johnny
Please rate answers -
How to determine the type of an existing iView
Hi,
While creating an iView using wizard there is a big list of type of iViews. But after creation of iView, how do I determine the type of iView. Which attribute or property of iView holds this information?
-Lave KulshreshthaYou can copy and paste the existing Iview changing the name and ID if u want to replicate the same.
Also, when you create a new iView from a template, you get a list of them e.g SAP BSP iView etc.
Now what you see in the code link is much similar to what you see as a list of templates when u create iViews. So its pretty simple.
I hope this helped you. -
How to count number of xml nodes , with JSTL XML api?
I am trying to count the number of nodes in the parsed xml by doing this:
<x:out select="count($doc/Items/Item)"/>
There are about 50 item nodes in the source xml, but I get the output as 0
Is this syntax correct?
I searched every where but couldn't find the xpath syntax to be used inside jstl xml tags, I was assuming that it's the same as the syntax used in xsltThank you for your reply, sorry I wasnt very clear with my question.
I want to count the number of nodes inside the jsp file , not inside the xslt file.
My xml file is something like this for example:
<?xml version="1.0" encoding="UTF-8"?>
<DocumentRootNode>
<Products>
<Item>1</Item>
<Item>2</Item>
<Item>3</Item>
<Products>
</DocumentRootNode>
I want the count of number of Item nodes , in the JSP file.
I'm doing something like this in the JSP but it gives 0 instead of 3:
<c:import url="source.xml" var="xml" charEncoding="UTF-8"/>
<x:parse doc="${xml}" var="xml_doc"/>
<x:out select="count($xml_doc/Products/Item)"/>
null -
How to select a specific XML node with XPath
Hi,
I'm trying to a select a specific node from my XML data using the XPath Builder. For example, say I have a process variable 'xmlData' with the
following contents:
User 1
101
User 2
102
I can successfully use the following expression to retrieve the name of
a specific user:
(/process_data/xmlData/users/user[userid='102'])/name
The problem is I need to use another process variable for the selection
key. For Example:
(/process_data/xmlData/users/user[userid=/process_data/@userId])/name
But this always returns null. I've tried surrounding /process_data/@userId
with single quotes, double quotes, and event {$..$}.
Any help would be greatly appreciated.
Thanks.Hi,
Thanks for the quick replies. I gave a bit of a bad example, as the
userid in my data is really a string, not a number.
A better example is:
User 1
user1
User 2
user2
I can get the following to work perfectly:
(/process_data/xmlData/users/user[userid='user2'])/name
but I can't get it to work with
/process_data/@userId = "user2"
(/process_data/xmlData/users/user[userid=/process_data/@userId])/name
Thanks. -
How I can binding specified xml node to datagridview?
hello
please, i am new in c#;
I have xml file, i displayed the first node within form in c#, this node have attribute is called 'id', now i want to display all nodes that have attribute is called 'father' its value equal to id value of the displayed node.
xml file:
<?xml version="1.0" encoding="UTF-8"?>
<tree>
<grand name="aaa" id="1" sex="m" status="d" child="2" father=""></grand>
<grand name="ddd" id="12" sex="m" status="d" child="" father="1"></grand>
<grand name="bbb" id="11" sex="m" status="d" child="1" father="1"></grand>
<grand name="ccc" id="111" sex="m" status="d" child="1" father="11"></grand>
<grand name="ddd" id="1111" sex="m" status="d" child="3" father="111"></grand>
<grand name="eee" id="11111" sex="f" status="d" child="" father="1111"></grand>
<grand name="fff" id="11112" sex="m" status="d"child="" father="1111"></grand>
</tree>
c# code that display the first node is:
InitializeComponent();
XDocument xdoc = XDocument.Load(" http://domaim.net/tree/index.xml");
var name = xdoc.Descendants("grand").First().Attribute("name").Value;
var sex = xdoc.Descendants("grand").First().Attribute("sex").Value;
var alive = xdoc.Descendants("grand").First().Attribute("status").Value;
var child = xdoc.Descendants("grand").First().Attribute("child").Value;
var id = xdoc.Descendants("grand").First().Attribute("id").Value;
var father = xdoc.Descendants("grand").First().Attribute("father").Value;
textBox1.Text = name;
if (sex == "m") textBox2.Text = "male"; else textBox2.Text = "female";
if (child == "") textBox4.Text = "no child"; else textBox4.Text = child;
if (alive == "d") textBox3.Text = "dead"; else textBox3.Text = "alive";
here I displayed the node its id="1", now i want to display all nodes that its "father" attribute is equal 1. i want to display thier attributes of these nodes within datagridview,(bbb & ddd).
thank you for all.Try this. You have an error in the XML that you need to put a space before child in the last row.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
public partial class Form1 : Form
public Form1()
InitializeComponent();
DataSet ds = new DataSet();
ds.ReadXml(@"c:\temp\index.xml");
dataGridView1.DataSource = ds.Tables["grand"];
DataTable fatherRows = ds.Tables["grand"].AsEnumerable()
.Where(x => x.Field<string>("father") == "1").CopyToDataTable();
dataGridView2.DataSource = fatherRows; -
How can i append new xml node to xmltype doc with updatexml and xpath?
ex. content of column:
<xmldoc>
<xmlnode attr="x" value="x1"/>
<xmlnode attr="y" value="y1"/>
</xmldoc>
needed to be:
<xmldoc>
<xmlnode attr="x" value="x1"/>
<xmlnode attr="y" value="y1"/>
<xmlnode attr="z" value="z1"/>
<xmldoc/>
how can i do this with updatexml without rewriting all xmlnode-elements?You have to specify the DTD in the transformation. If you are using XSLT then you do that on your <xsl:output>. Otherwise you need to give more details.
-
How to determine if a file is existing
Hi,
do we have a way to detect if a is existing in java? apparently I use RandomFileAccess to load my file however, I'd like to check in advance if the file is existing already.I use RandomFileAccess to load my file however, I'd like to check in advance if the file is existing already.Bad idea. Pointless waste of time and space. What if the file exists when you pre-check but isn't there when you open it?
Just catch the FileNotFoundException that the RandomAccessFile will throw. Much simpler. You have to catch IOExceptions anyway ... -
Convertion of String to XML node using Xquery transformation in OSB
How to convert string to XML node elementusing a built in function using Xquery transformation in OSB?
check this out - http://www.javamonamour.org/2011/06/fn-beainlinedxml.html
if in SOA (BPEL & Mediator) you can use oraext:parseXML.
you should thoroughly analyse where to implement your requirement as some good practices advise to implement more complex logic in SOA and leave OSB to only connect to the services' endpoints.
Hope this helps,
A. -
Check whether there is nodes inside a xml node
I am trying a few different way, but i can't get the point,
hope some expert can help me on this.
eg:
<product>
<item>
</item>
</product>
<cfscript>
<!--- after xml parse to xmlElement --->
if (xmlElement.product.item neq ""){
writeOutput(xmlElement.product.item[1]);
But this wont work for me, anyone know how to check whether a
xml node contains child nodes or not?xmlSearch()?
Adam -
Call subtemplate if the corresponding node exists in the XML
Hi,
I have a requirement to call the sub template if the corresponding exists in the XML file. For example if the invoice node and the corresponding details are present in the XML file then i have to call the invoice sub template. Can any one suggest me how to check if the node exists or not in the XML file. Is there any specific command for this.
ThanksHi,
Thanks a lot..it is working now as per ur suggestions..but am facing another problem here. Am calling the sub template from a main template. Headers and footers for both main and sub templates are different. So for calling the sub template i did section break (Insert -->Break , In section break-->New page) so that the header and footer of the main template wll not be repeated in the sub template. So in the new page am checking if the node exists or not the XML. If so then call the XML. If the node exists then sub template is called successfully. But if the node does not exists then a blank page is displayed for this since the call stmt is in the new page.
So can you please suggest me how to suppress the blank page if the node does not exist.
Thanks. -
How to check whether a node exist in a Particular Level. (xmltype)
hi,
please help me to check whether a particular node exists in one level.
for eg
I have the following xml
<map>
<entry>
<key>
heading1
</key>
<map>
<entry>
<key> sub1 heading1</key>
<value> sub1 heading1 value </value>
</entry>
<entry>
<key> sub2 heading1 </key>
<value> sub2 heading1 value </value>
</entry>
</map>
</entry>
<entry>
<key>
heading2
</key>
<map>
<entry>
<key> sub1 heading2</key>
<value> sub1 heading2 value </value>
</entry>
<entry>
<key> sub2 heading2 </key>
<value> sub2 heading2 value </value>
</entry>
</map>
</entry>
</map>i need to check how many heading exists in this xml.
I am checking like
i:=1;
l_section := ip_xml.extract('//map/entry');
WHILE l_section.existsnode('entry[' || i|| ']') = 1 LOOP
// extract the key name within entry tag
// print the key name.
i:=i+1;
end loop;but iam getting all the key name like
heading1
sub1 heading1
sub2 heading1
heading2
sub1 heading2
sub2 heading2
I need only heading1 and heading2. how can I check whether a particular node exist in particular level.
first level , second level etc. Please helpbut iam getting all the key nameThat's because you're using a descendant axis : //map/entry
SQL> DECLARE
2
3 ip_xml xmltype := xmltype('<map>
4 <entry>
5 <key>
6 heading1
7 </key>
8 <map>
9 <entry>
10 <key> sub1 heading1</key>
11 <value> sub1 heading1 value </value>
12 </entry>
13 <entry>
14 <key> sub2 heading1 </key>
15 <value> sub2 heading1 value </value>
16 </entry>
17 </map>
18 </entry>
19 <entry>
20 <key>
21 heading2
22 </key>
23 <map>
24 <entry>
25 <key> sub1 heading2</key>
26 <value> sub1 heading2 value </value>
27 </entry>
28 <entry>
29 <key> sub2 heading2 </key>
30 <value> sub2 heading2 value </value>
31 </entry>
32 </map>
33 </entry>
34 </map>');
35
36 BEGIN
37
38 for r in (
39 select heading, headno
40 from xmltable( '/map/entry/key'
41 passing ip_xml
42 columns heading varchar2(30) path '.'
43 , headno for ordinality )
44 )
45 loop
46 dbms_output.put_line('Heading '||r.headno||' = '||r.heading);
47 end loop
48 ;
49
50 END;
51 /
Heading 1 =
heading1
Heading 2 =
heading2
PL/SQL procedure successfully completed -
How to create xml nodes based on a value
Dear friends,
I've a question about graphical mapping in SAP PI...
How can I create XML nodes on the target side based on a value in a XML field on the source side.
For example:
This XML field on the source:
<NO_OF_LINES>4</NO_OF_LINES>
Must result on 4 Lines on the Target:
<LINE></LINES>
<LINE></LINES>
<LINE></LINES>
<LINE></LINES>
So it's actually the opposite of the Count function...
I appreciate your help,
Thank you in Advance,
Kind regards,
JohnHi ,
Try this
NO_OF_LINES---> count---> UDF---> LINE
example :
UDF Code :
for (int i=0;i<var1[0];i++)
result.addValue(""); -
Xml in JTree: how to not collpase JTree node, when renaming XML Node.
Hi.
I'm writing some kind of XML editor. I want to view my XML document in JTree and make user able to edit contents of XML. I made my own TreeModel for JTree, which straight accesses XML DOM, produced by Xerces. Using DOM Events, I made good-looking JTree updates without collapsing JTree on inserting or removing XML nodes.
But there is a problem. I need to produce to user some method of renaming nodes. As I know, there is no way to rename node in w3c DOM. So I create new one with new name and copy all children and attributes to it. But in this way I got a new object of XML Node instead of renamed one. And I need to initiate rebuilding (treeStructureChanged event) of JTree structure. Renamed node collapses. If I use treeNodesChanged event (no rebuilding, just changes string view of JTree node), then when I try to operate with renamed node again, exception will be throwed.
Is there some way to rename nodes in my program without collpasing JTree?
I'am new to Java. Maybe there is a method in Xerces DOM implementation to rename nodes without recreating?
Thanks in advance.I assume that "rename" means to change the element name? Anyway your question seems to be "When I add a node to a JTree, how do I make sure it is expanded?" This is completely concerned with Swing, so it might have been better to post it in the Swing forum, but if it were me I would do this:
1. Copy the XML document into a JTree.
2. Allow the user to edit the document. Don't attempt to keep an XML document or DOM synchronized with the contents of the JTree.
3. On request of the user, copy the JTree back to a new XML document.
This way you can "rename" things to the user's heart's content without having the problem you described.
Maybe you are looking for
-
HI there, ive written a class to display a Jframe with a "next button to generate a new pane withoin the frame, rather like a wizard setup. the trouble is, when I hit next, the pane doesnt update proberly and only displays the card of the previous pa
-
Strange alpha channel behavior in jscroll pane
Hi, My first post here. I have been strugling for a while with a problem with alpha channel opacity and scrollpane. I basically have a JScrollpane that contains a JPanel with JLabels. The JLabels have a mouse action that changes their background to h
-
Hi, We have requirement to disply images with .cgm extension in iStore. Based on my research, found that CGM images cannot be viewed directly on a browser unless we have some software or plug-ins. Any thoughts on how to enable viewing of CGM images i
-
The default font for the "Centered" title option appears to be Gils Sans. Does anyone know how to set a different default font, perhaps either for that title option alone or for all title options? Not a big deal, but since I always use Arial for my t
-
How execute java program through crontab in linux
i have written in crontab like * * * * * cd /usr/java/jdk1.5.0/bin;pwd;echo $PATH; ./test2.sh 2>logjava1 test2.sh contains java samle in logjava1 the error is java :command not found Please help me .this is not working