Solved

returning the attribute value in a XML file

Posted on 2004-08-17
8
202 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

803 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