Solved

returning the attribute value in a XML file

Posted on 2004-08-17
8
200 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now