Solved

returning the attribute value in a XML file

Posted on 2004-08-17
8
203 Views
Last Modified: 2013-11-19
I have an XML file in this format,

<Instance id="Company" type="GAS">
      <profile name="Aquila" duns="006966055">
        <dataflow option="nngdefault">
            <in id="NOMS"/>
                <in id="FNAK"/>
            <out id="NQRS"/>
            <out id="MVSO"/>
            <out id="SCHQ"/>
                <out id="OSQO"/>
         </dataflow>
       </profile>

      <profile name="BP Canada" duns="248799413">
         <dataflow option="nngdefault">
            <out id="NQRS"/>
            <out id="SCHQ"/>
            <in id="NOMS"/>
            <in id="FNAK"/>
         </dataflow>
      </profile>
</Instance>

I am to pass parameters profile name( BP Canada) and if it exists, I want the method to return the value of duns attribute(for eg: 248799413). Can this be possbile using Xpath? if yes can some one help me with this? it is not mandatory to be in Xpath. Any help would greatly be appreciated.

Thanks

0
Comment
Question by:g46905
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 150 total points
ID: 11824303
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11824323
AS i mentioned in your earlier question, rather than boolean, make the validation method return a node, then you can get the duns attribute
0
 

Author Comment

by:g46905
ID: 11824387
CEHJ,
       I am trying to get the value in the node by check the profile name. Please keep in mind that I am new to java (2 to 3 months) and try to help me with this. Thanks for your concern and time.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11824404
using SAX


final class MyXML
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ContentHandler


{
   
    String duns;
    String getDuns()
    {
        return duns;
    }
    final private static void print
  ( final String context, final String text )
  { System.out.println( context + ": \"" + text + "\"." ); }

  final public void startElement
  ( final String namespace, final String localname,  
    final String type, final org.xml.sax.Attributes attributes )
  throws org.xml.sax.SAXException
  {
      if(localname.equals ("Profile"))
      {
          String name=attributes.getValue("name");
          if(name!=null )
          {
              duns=attributes.getValue("duns");
          }
      }
      print( "startElement", type ); }

  final public void endElement
  ( final String namespace, final String localname,  
    final String type )
  throws org.xml.sax.SAXException
  { print( "endElement  ", type ); }

  final public void characters
  ( final char[] ch, final int start, final int len )  
  { final String text = new String( ch, start, len );
    final String text1 = text.trim();
    if( text1.length() > 0 )print( "characters  ", text1 ); }}
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 11826005
That be about correct.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 11828862
Using the suggestion i gave you earlier about not returning true or false from the initial validation mwthod, but Node or null, you can use this for returned node 'n':


    if (n != null) {
      n = n.getParentNode().getParentNode();
      System.out.println(n.getAttributes().getNamedItem("duns"));
    }


When i have a little more time, i'll post a more XPath-like implementation
0
 

Author Comment

by:g46905
ID: 11830262
Thank for your suggestion and valuable time.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11851756
:-)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question