0

I have a XML file

<properties>
  <setting>
    <group>A</group>
    <name>John</name>
    <job>Manager</job>
  </setting>
  <setting>
    <group>B</group>
    <name>Peter</name>
    <job>Admin</job>
  </setting>
</properties>

Next my code will read in from URL, split the text and add to arraylist

ArrayList<Employee> pArray = new ArrayList<SettingForm>();
try {
    URL url = new URL(urlLink);
    BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;

    while ((line= input.readLine()) != null){
        String[] value = line.split("=");
        if(value.length > 1){
            pArray .add(new Employee(value[0], value[1], group, name, job));
        }
    }
    input.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
}

After adding the value[0] and value[1] to the arraylist, I also need to take in the XML attributes of group, name and job and add in to the arraylist. How do I need to do to achieve this?

f1sh
  • 11,489
  • 3
  • 25
  • 51
Ken.T
  • 9
  • 5
  • With this current code, you will not be getting any results and the ArrayList will be empty. – f1sh Dec 13 '17 at 02:43
  • @f1sh what do you mean? – Ken.T Dec 13 '17 at 02:47
  • you only add data to the list if ``value.length > 1`` which never happens because you don't have a single line that contains a ``=`` character. – f1sh Dec 13 '17 at 02:48
  • @f1sh. oh, actually it does have line that contains = character. Just that I never show. From the above example, I am only specify the link. But inside the link there is line that contains = character. – Ken.T Dec 13 '17 at 02:52
  • What happens when you open a connection to the underlined URL? – Roman C Dec 20 '17 at 17:50

1 Answers1

0

You can use Java's built in XML parser in the the javax.xml.parsers package to load the XML into a document, then loop over the NodeList to find the right attributes. Add the right attributes to a list in array form and return the list's iterator when finished:

public static Iterator<String> getAttributes(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    ArrayList<String> attributes = new ArrayList<String>();
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);
    /*
     * optional, but recommended read this -
     * http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with
     * -java-how-does-it-work
     */
    doc.getDocumentElement().normalize();

    NodeList nList = doc.getElementsByTagName("setting");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;

            String group = eElement.getElementsByTagName("group").item(0).getTextContent();
            String name = eElement.getElementsByTagName("name").item(0).getTextContent();
            String job = eElement.getElementsByTagName("job").item(0).getTextContent();

            attributes.add(group);
            attributes.add(name);
            attributes.add(job);
        }
    }
    return attributes.iterator();
}

Now all you have to do is call getAttributes() before reading the URL and use Iterator#next() to get the info in the correct order:

    Iterator<String> iterator;
    try {
        iterator = getAttributes(new File("..."));
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
        return;
    }
    ArrayList<Employee> pArray = new ArrayList<SettingForm>();
    try {
        URL url = new URL(urlLink);
        BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;

        while ((line= input.readLine()) != null){
            String[] value = line.split("=");
            if(value.length > 1){
                pArray .add(new Employee(value[0], value[1], iterator.next(), iterator.next(), iterator.next()));
            }
        }
        input.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
    }   
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • HI, thanks for the reply! From your answer, I think you are mistaken, My requirements need to read in an URL and a XML file, After reading the URL, the text in the URL is split into 2, thats why there is value[0] and value[1]. And the XML attributes are also needed to add into the arraylist, forming: employees.add(new Employee(value[0], value[1], group, name, job)); – Ken.T Dec 13 '17 at 05:49
  • @Ken.T so does the URL point to the XML file, or is the XML file separate? – Cardinal System Dec 13 '17 at 06:26
  • The URL and the XML file is separate. But both got values needed to be added into the arraylist – Ken.T Dec 13 '17 at 06:41
  • Cardinal System, do you have any ideas on how to do it? – Ken.T Dec 14 '17 at 10:04
  • Hi Cardinal System, I have seem your updated answer. If i wants to add group, name and job to the "employees.add(new Employee(value[0], value[1], group, name, job))" by comparing the value of value[0] and "group" if it matches. How should I do it? By using the above method, i cannot compare as it is using iterator.next(). – Ken.T Dec 15 '17 at 10:48
  • @Ken.T why don't you try something? In this case, it really boils down to common sense. If the `Emplyee` constructor only takes String arguments, you can easily figure out how to save an instance for comparison. Your question was on getting XML attributes, and I gave a working answer. Anyone with a basic knowledge of Java could figure out how to do [this](https://hastebin.com/ivuqasefeg.vbs) on their own. I dissuade you from trying more complicated things like XML and HTML parsing if you can't even guess how to clone instances from an iterator. – Cardinal System Dec 15 '17 at 23:12
  • Hi Cardinal, Thanks! I think i might have missed out this simple basic knowledge. Yea, the answer given is the right one. – Ken.T Dec 18 '17 at 02:51