Solved

returning the attribute value in a XML file

Posted on 2004-08-17
8
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

632 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