简体   繁体   中英

Read data from XML file (in Java) using Reflection

Given a simple XML file , like :

<Game>
    <Round>
        <roundNumber>1</roundNumber>
        <Door>
            <doorName>abd11</doorName>
            <Value>
                <xVal1>0</xVal1>
                <xVal2>25</xVal2>
                <pVal>0.31</pVal>
            </Value>
            <Value>
                <xVal1>25</xVal1>
                <xVal2>50</xVal2>
                <pVal>0.04</pVal>
            </Value>
            <Value>
                <xVal1>50</xVal1>
                <xVal2>75</xVal2>
                <pVal>0.19</pVal>
            </Value>
            <Value>
                <xVal1>75</xVal1>
                <xVal2>100</xVal2>
                <pVal>0.46</pVal>
            </Value>
        </Door>
        <Door>
            <doorName>vvv1133</doorName>
            <Value>
                <xVal1>60</xVal1>
                <xVal2>62</xVal2>
                <pVal>1.0</pVal>
            </Value>
        </Door>
    </Round>
    <Round>
        <roundNumber>2</roundNumber>
        <Door>
            <doorName>eee</doorName>
            <Value>
                <xVal1>0</xVal1>
                <xVal2>-25</xVal2>
                <pVal>0.31</pVal>
            </Value>
            <Value>
                <xVal1>-25</xVal1>
                <xVal2>-50</xVal2>
                <pVal>0.04</pVal>
            </Value>
            <Value>
                <xVal1>-50</xVal1>
                <xVal2>-75</xVal2>
                <pVal>0.19</pVal>
            </Value>
            <Value>
                <xVal1>-75</xVal1>
                <xVal2>-100</xVal2>
                <pVal>0.46</pVal>
            </Value>
        </Door>
        <Door>
            <doorName>cc</doorName>
            <Value>
                <xVal1>-60</xVal1>
                <xVal2>-62</xVal2>
                <pVal>0.3</pVal>
            </Value>
            <Value>
                <xVal1>-70</xVal1>
                <xVal2>-78</xVal2>
                <pVal>0.7</pVal>
            </Value>
        </Door>
    </Round>
</Game>

I want to read data from that file .

I can do it in the old fashioned way of reading the tags , and create the objects accordingly ,

but I want to do it using Reflection mechanism .

Can someone please explain or direct me to a tutorial that can explain how to do that ?

Thanks

EDIT:

I did the following :

import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

public class ReadAndPrintXMLFile {


    public static void main (String argv []){
    try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File("input.xml"));


            // ROOT 

            // normalize text representation
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " + 
                 doc.getDocumentElement().getNodeName());


            // Number of nodes/rounds 


            NodeList listOfItems = doc.getElementsByTagName("Round");
            int totalElements = listOfItems.getLength();
            System.out.println("Total number of nodes : " + totalElements );
...
...
}

This is a partial code , I ran the code and tested it , and at the moment , using SAX , I can read from the XML file above .

Now , what I want to do is to manipulate the data using Reflection . From my understanding Reflection works only with EXISTING objects . Hence , is it enough to store the data (IE the rounds) in the variable listOfItems and now use Reflection on that ?

thanks again !

Well in that case I guess you can read the XML files into Java using Java XML api as paulsm4 suggested and use reflection to get them and manupulate them.

http://java.sun.com/developer/technicalArticles/ALT/Reflection/

This is dead so try this one. http://docs.oracle.com/javase/tutorial/reflect/index.html

This is not the best tutorial but it should be enough for what you need. And may I suggest that this is a very poor use of reflection API, and therefore I want to throw an egg at whoever gave you the homework assignment :-D

EDIT: Yes if you wish you can use reflection on that.. Or you can create an object model(Classes for Door, Round and Game) and populate them with values in the XML file and then use reflection on it to whatever that need to be done. It upto you to decide how best you want to do that!

Why don't you read it the "old fashioned way" of using one of the Java XML APIs?????

Google for "Java XML", "SAX", "DOM", "DocumentBuilder", "org.w3c.dom", etc and you'll find lots of good documentation and tutorials. For example:

You have something like:

GameRound >>
  int roundNumber
  Door door >>
    String doorName
    Value value >>
      int xVal1
      int xVal2
      float pVal

And you wish to parse your xml and populate your objects? Is this correct?

If yes, I don't see the point! Even if you don't want to use any api, write your own and populate the values via setters!

@Thihara +1 for the egg xD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM