Solved

returning the attribute value in a XML file

Posted on 2004-08-17
8
201 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

25 Experts available now in Live!

Get 1:1 Help Now