tags:

views:

47

answers:

3

I am getting xml file path from browse button and trying to parse but it is not working.. here is my code below. I am new to jQuery.. please help

$(document).ready(function(){
  alert("hi")
  $("#uploadButton").click(function(){
    var val = $("input#file").val()
    alert( val ) // it is displaying...
    $(val).find("CommandModule").each(function() {
       alert("inside cmdModule") // it is not displaying..
       $(this).find("CommandGroup").each(function() {
          $(this).find("CommandExecution").each(function() {
             $("#test").append($(this).attr("cmdId"));                 
          });
       }); // closing CommandGroup
    }); // closing CommandModule

 }); // closing Click function
}); // closing ready function

The XML in question (removed some sensitive info)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="qToutput2html-fancy.xsl"?>
<Host host="127.0.0.1" name="freebsd-01" xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:noNamespaceSchemaLocation="qToutput.xsd">
    <CommandModule modAuthor="Marcus Foo &lt;[email protected]&gt;" modDate="2009-09-18T00:00:00" modName="modFreeBSD" modVersion="0.1">
        <CommandGroup grpHeading="System Information" grpId="cmdgrp0" grpName="">
            <CommandExecution cmdDiff="true" cmdFlags="" cmdHeading="NetoHostname" cmdId="cmd0" cmdName="QtReport" cmdRefText="" cmdRefUrl="" cmdReqId="" cmdSu="false">
                <TimeStart>2010-03-11T11:10:42.067000</TimeStart>
                <TimeStop>2010-03-12T12:20:42.114000</TimeStop>
                <Command>
                    <Line>Neto</Line>
                </Command>
                <Stdout>
                    <Line>abc.cab.cst.com</Line>
                </Stdout>
                <Stderr/>
                <ExitCode>5</ExitCode>
            </CommandExecution>
        </CommandGroup>
    </CommandModule>
</Host>
A: 

$(val).find("CommandModule").each(function () { is probably failing because your XML isn't valid, or because there is not node by that name.

roosteronacid
Here below is my xml file..<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="qToutput2html-fancy.xsl"?><Host host="192.168.56.101" name="freebsd-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="qToutput.xsd"> <CommandModule modAuthor="Marcus Haebler <[email protected]>" modDate="2009-09-18T00:00:00" modName="modFreeBSD" modVersion="0.1"> <CommandGroup grpHeading="System Information" grpId="cmdgrp0" grpName="">
Ravi
<CommandExecution cmdDiff="true" cmdFlags="" cmdHeading="NetoHostname" cmdId="cmd0" cmdName="QtReport" cmdRefText="" cmdRefUrl="" cmdReqId="" cmdSu="false"> <TimeStart> 2010-03-11T11:10:42.067000 </TimeStart> <TimeStop> 2010-03-12T12:20:42.114000 </TimeStop> <Command> <Line> Neto </Line> </Command>
Ravi
<Stdout> <Line> abc.cab.cst.com </Line> </Stdout> <Stderr/> <ExitCode> 5 </ExitCode> </CommandExecution> </CommandGroup> </CommandModule></Host>
Ravi
A: 

I am able to get this to work when I load the xml file using .load rather than uploading it or however you are accessing your xml file. Here is the code I used:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title> Test </title>
        <link href="main.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../intranet/include/js/jQuery/jquery-1.4.1.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
              alert("hi")
              $("#uploadButton").click(function(){
                  alert("clicking");
                $("#input_file").load("testxml.xml", {}, function(xml){
                    $(xml).find("CommandModule").each(function() {
                        alert("inside command module");
                        $(this).find("CommandGroup").each(function(){
                            $(this).find("CommandExecution").each(function() {
                                $("#test").append($(this).attr("cmdId"));
                                });
                            });
                        });
                    });
             }); // closing Click function
            }); // closing ready function
        </script>
    </head>
    <body>
        <div style="margin: 0 auto; width: 822px;">
        <div id="uploadButton">Buttons!</div>
        <div id="input_file"></div>
        <div id="test"></div>
        </div>
    </body>
</html>
caligoanimus
I used your code but I got 404 error like below:<html><head><title>Apache Tomcat/6.0-snapshot - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-
Ravi
color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-
Ravi
color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /ReportingForQt/qtReport/qtSample.xml</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/ReportingForQt/qtReport/qtSample.xml</u></p><p>
Ravi
<b>description</b> <u>The requested resource (/ReportingForQt/qtReport/qtSample.xml) is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0-snapshot</h3></body></html>
Ravi
I had the xml file hosted on the server. 404 is most likely because the xml file is not in the location you are looking for.
caligoanimus
A: 

To make things short. Your XML is not wellformed check the tag Host

<Host
    host="127.0.0.1"
    name="freebsd-01"
    xmlns:xsi="w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="qToutput.xsd"
>

Do you notice what is wrong? There is a semi-colon after the xmlns:xsi attribute which isn't allowed.

xmlns:xsi="w3.org/2001/XMLSchema-instance";

xmlns:xsi="w3.org/2001/XMLSchema-instance"

jitter
when I checked my file there was no semicolon present in that line..
Ravi