0

I have an XML file that I am trying to search using Java. I need to find an element by its attribut value (Port number) and return the depending description of the element.

The xml file of known ports should be hosted online and has an architecture like:

<ports>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
</ports>

The elements have no unique identifier. In my application i want to call a function with a number as parameter and it shoult give me the description of the item with the given attribute "number".

My problem is, its a list of all known ports and i cannot manually edit the structure to assign all elements with the portnumber as attribute. can anyone show me how to solve that?

thanks in advance

UPDATE: i want to search it like get_port(int portno). thats my code:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMExampleJava {

    public static void main(String args[]) {

        try {

            File input = new File("input.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(input);
            doc.getDocumentElement().normalize();

            NodeList nodes = doc.getElementsByTagName("record");
            System.out.println("==========================");

                for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);

                    if (node.getNodeType() == Node.ELEMENT_NODE) {

                        Element element = (Element) node;
                        System.out.println("Port number: " + getValue("number", element));
                        System.out.println("Protocol: " + getValue("protocol", element));
                        System.out.println("Description: " + getValue("description", element));

                    }

                }

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

    private static String getValue(String tag, Element element) {

        NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = (Node) nodes.item(0);
        return node.getNodeValue();

    }

}
user4438328
  • 35
  • 1
  • 11
  • You need to use an [XML parser](https://www.google.com/search?q=java+xml+parser) to parse XML. – dimo414 Aug 03 '15 at 23:52
  • You will need an XML parser. I personally like to use [Jsoup](http://jsoup.org/) – Shreyas Chavan Aug 04 '15 at 00:03
  • Ok thank you i got it. i have updated my question with my new code. could you also help me to search for a specific record in it depending on attribute number? – user4438328 Aug 04 '15 at 00:05
  • 2
    Have you tried [using XPath](http://stackoverflow.com/q/6538883/113632)? – dimo414 Aug 04 '15 at 00:15
  • the problem is, as described, that i have a ready xml list of known ports (about 40000 elements) and its architecture is also without unique identifier for the elements. i use to search it by the elements attribute values not the elements values! so every element has 3 attributes but no unique identifier and i dont know how to search the description to a given port number. – user4438328 Aug 04 '15 at 00:20
  • Why are you doing this in Java? It would be so much easier to use XPath, XSLT, or XQuery, all of which can be called from Java. – Michael Kay Aug 04 '15 at 07:20

1 Answers1

1

Create your own HashMap obj inside the method and you can get the record you want from the list. E.g. HashMap<int itemId, List<item>> yourOwnItem = new HashMap<>(); At the end of the for loop pass the item to yourOwItem as follows:
yourOwnItem.put(i , List<item>);

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Want2bExpert
  • 527
  • 4
  • 11
  • Sry when i am shameless but could you show me your code? I dont understand from where to get the List variable and how to search it. – user4438328 Aug 04 '15 at 00:27
  • OK. Create a DataModel class that represent each item elements, call the setters of that class where you use system.out.print on(). Add the object of that class to a List obj e.g. List the add the List to HashMap using the PortNumber as the Key. – Want2bExpert Aug 04 '15 at 00:33
  • Ok i think i understand your description, i also found a similar way on google with an array but i didnt got it. i will see if i will find a solution. thank you anyway. – user4438328 Aug 04 '15 at 00:37
  • Glad I could help. You can follow the steps I typed early and use array in place of HashMap e.g. ArrayList – Want2bExpert Aug 04 '15 at 00:42
  • Just recognized that i havent voted up. Nice answer! – user4438328 Jun 23 '18 at 19:58