0

I've been looking for the past few hours, and I can't find how to do it.

My XML file:

<list>
 <Company id="01">
  <Name>Atari</Name>
  <Founded>1972</Founded>
  <Consoles>
   2600
   5200
   7800
  </Consoles>
 </Company>
 <Company id="02">
  <Name>Sega</Name>
  <Founded>1960</Founded>
  <Consoles>
   Master System
   Megadrive
   Saturn
  </Consoles>
 </Company>
</list>

Basically, I want the code to find not only the name in one company block, but the name in any company block I wish, and be able to show it to other classes. So far, I've been able to show either or, but only by changing the code directly and not on the fly. The code I'm using:

    private static String getTextValue(String def, Element doc, String tag) {
    String value = def;
    NodeList nl;
    nl = doc.getElementsByTagName(tag);
    if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
        value = nl.item(0).getFirstChild().getNodeValue();
    }
    if(value==null) value = " ";
    return value;
}

public static boolean readXML(String xml) {
    rolev = new ArrayList<String>();
    Document dom;
    // Make an  instance of the DocumentBuilderFactory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // use the factory to take an instance of the document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // parse using the builder to get the DOM mapping of the    
        // XML file
        dom = db.parse(xml);

        Element doc = dom.getDocumentElement();

        role1 = getTextValue(role1, doc, "Name");
        if (getRole1() != null) {
            if (!getRole1().isEmpty())
                rolev.add(getRole1());
        }
        role2 = getTextValue(role2, doc, "Founded");
        if (role2 != null) {
            if (!role2.isEmpty())
                rolev.add(role2);
        }
        role3 = getTextValue(role3, doc, "Consoles");
        if (role3 != null) {
            if (!role3.isEmpty())
                rolev.add(role3);
        }
        role4 = getTextValue(role4, doc, "Name");
        if ( role4 != null) {
           if (!role4.isEmpty())
                rolev.add(role4);
        }
        return true;

    } catch (ParserConfigurationException pce) {
        System.out.println(pce.getMessage());
    } catch (SAXException se) {
        System.out.println(se.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    return false;
}

I used a lot of different methods, some searched up and some made on my own, but this one is the closest I can get to working for what I need. I need to be able to have the place it's looking change on the fly though.

  • 2
    Have you tryed to implement it using XPath? This example may help to get started: http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml – Fernando Costa Aug 21 '15 at 01:04

1 Answers1

0

You can use xpath to read XML. See Parsing XML with XPath in Java and/or How to read XML using XPath in Java

In your case, if you wanted to get all of the company names the set path you would use (basically the tag filter) would be "//list/Company/Name/text()"

The rest of the code can basically be copied from the questions I posted. Only your set (filter) would be different.

Community
  • 1
  • 1
Zarwan
  • 5,537
  • 4
  • 30
  • 48