troubleshooting Question

Help With the object model of Java

Avatar of anshuma
anshumaFlag for United States of America asked on
Java
16 Comments1 Solution243 ViewsLast Modified:
Hi,

I need to parse the following

xml files (xml1 and xml2)

I am opening them in the SAX Parser but I am unable to figure out a generalized way of traversing. I want to make sure that I fully traverse the tree in a very general way as the xml nodes can increase and decrease as shown in the two files.

My Trial code for Java is attached in CodeForPrintingtheparsedXML.txt


I have been successful in doing this in PHP by the following code. I hope a similar object model exists in Java. My PHP Code is below


_______________________________________________________________________


// A DELIMITER
$dlm = '|';

// MAKE AN OUTPUT BUFFER
$outattrib = NULL;
$outmetric = NULL;
$outreportdata=NULL;


// MAKE AN OBJECT
$obj = SimpleXML_Load_String($reportxmldata);


foreach ($obj->report_data_list->report_data->objects->attribute as $object)
{
    $outattrib
    .= (string)$object["name"].$dlm.PHP_EOL;
}


foreach ($obj->report_data_list->report_data->objects->metric as $object)
{
    $outmetric
    .= (string)$object["name"].$dlm.PHP_EOL;
}



$outreportdata.="<br/>";

foreach ($obj->report_data_list->report_data->raw_data->rows->r as $row)
{
   
   
   foreach ($row->v as $robj)
    {
           
        $outreportdata.= (string)$robj.$dlm.PHP_EOL;            
         
             
    }
   
   $outreportdata.="<br/>";
   
}


// SHOW All the Attributes
$outattrib = trim($outattrib);
print_r($outattrib);


// SHOW All the Metrics
$outmetric = trim($outmetric);
print_r($outmetric);




// SHOW All the report_data
$outreportdata = trim($outreportdata);
print_r($outreportdata);

_______________________________________________________________________
xml1.txt
xml2.txt
parse-xml1.txt
parse-xml2.txt
CodeForPrintingtheparsedXML.txt
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 16 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros