Results 1 to 5 of 5
  1. #1
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Read xml data with multiple attributes and store values

    Hi all,
    I have xml file with the following format to read and store the values.
    I am very new to Java and trying my best to solve this issue.

    XML file format:

    <ip address="1.1.1.1" consecutive_listings="2" listing_ratio="2" reputation="9">
    <attacks hostility="4" confidence="5" />
    <malware hostility="3" confidence="3" />
    </ip>

    I have following code to read the xml but I am very confused with the multiple attributes and do not know how to proceed.

    private static String getXMLTagValue(String xml, String tag) {
    if ((xml == null) || (xml.length() == 0))
    return "-1";
    String startTag = "<" + tag + ">";
    String endTag = "</" + tag + ">";
    int start = xml.indexOf(startTag);
    if (start != -1) {
    start += startTag.length();
    }
    int end = xml.indexOf(endTag);

    if ((start != -1) && (end != -1) && (end > start)) {
    return xml.substring(start, end);
    }
    return "-1";
    }

    Could you please help me with:
    1. How do I read xml with multiple attributes?
    2. What is the best way to store these xml values and then compare it with Database.

    I will really appreciate your help on this.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    There are three main ways to read XML in Java: DOM, SAX, and XPath. DOM creates a Document object you can navigate through to get your tags and attributes; it's useful when you're interested in most or all of the data. SAX fires events as it parses the XML, and you write your own event handler to do different things with the events. XPath uses a very simple syntax to get specific tags from the file, and is most useful when you're only interested in a few things.

    Google each of these along with the word 'java' to find out more.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks a bunch Kjkrum!
    I will definitely read more on DOM, SAX and XPath to read xml.
    If possible could you please also tell me what is the best way to store these read values and then compare with the database.
    Any direction will greatly help me. I am pretty new so thats why these questions.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    I don't know a lot about DB programming. It seems you would have to start by answering a few questions:

    1. How does the data in the XML map to the data in the DB? Like, which element or attribute maps to which table and column.
    2. What are you interested in comparing -- whether some rows match the data from the XML, or all rows match, or what?
    3. What are you going to do if they don't match? Which is authoritative, the DB or the XML?
    Get in the habit of using standard Java naming conventions!

  5. #5
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Actually its little complicated but I will try my best to explain it.

    XML to be used:
    <ip address="1.1.1.1" consecutive_listings="2" listing_ratio="2" reputation="9">
    <attacks hostility="4" confidence="5" />
    <malware hostility="3" confidence="3" />
    </ip>

    We have following data in DB and needs to be mapped with the read xml data
    IP_Start| IP_End | RISK

    1. Following are few data columns I need to map with the xml elements/attributes

    IP_Start -> <stores long format of IP > in the code I have to convert dot IP in xml to Long and map to this column
    IP_End -> same as above
    RISK-> <Number is stored> A calculated number based on a specific formula get stored here.
    The formula uses attributes values such as hostility, reputation, confidence etc per Attacks, Malware elements.

    2. The XML data needs to be converted after read before I match it with the database.
    3. XML is the authoritative in this case

Similar Threads

  1. Need a solution to read and store data from a file
    By sheetalnri in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 07:43 AM
  2. How to read data from excel and store it in db
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-03-2008, 11:19 AM
  3. Replies: 40
    Last Post: 07-04-2008, 09:41 AM
  4. How to read AIX attributes & values.
    By John_28 in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 09:07 AM
  5. How to read attributes and values in a xml file using servlet
    By pragathi_forum in forum Advanced Java
    Replies: 1
    Last Post: 12-18-2007, 06:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •