Question:
Java, how to read an xml file for a string?
Owen
2012-08-19 13:38:03 UTC
I am trying to read an xml file that has a string stored in it. The string is stored in the following way:

What this program needs to do is take the string called "Hey" from the xml file and assign the value of "Hi" to it. I don't know how to do that in java, so if anyone could help, it would be much appreciated.
Thank you.
Four answers:
Imad Eddin
2012-08-19 14:08:59 UTC
Hi here are the classes you need to do this:

1 -

package xml;



import java.io.File;



import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;



public class XMLReader {



public static void main(String[] args) {



try {



File file = new File("Path to DataFile.xml");

JAXBContext jaxbContext = JAXBContext.newInstance(HiModel.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();



HiModel hiModel = (HiModel) jaxbUnmarshaller.unmarshal(file);



System.out.println(hiModel.getName());

System.out.println(hiModel.getValue());



} catch (JAXBException e) {

e.printStackTrace();

}



}

}



2 - package xml;



import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlRootElement;



@XmlRootElement(name = "string")

public class HiModel {



private String name;

private String value;



public HiModel() {

}



public String getName() {

return name;

}



@XmlAttribute(name = "name")

public void setName(String name) {

this.name = name;

}



public String getValue() {

return value;

}



@XmlAttribute(name = "value")

public void setValue(String value) {

this.value = value;

}



}



3 -



Jared
2012-08-19 14:03:46 UTC
I think a SAX parser is the simplest way to parse XML files. Unfortunately, this method is pretty convoluted in Java (there is another way which uses a DOM tree, but I don't know how to do that).



Alright, here are the general steps for creating a SAXParser:



1) Create a SAXParserFactory

2) Use that factory to create a SAXParser

3) create a Handler for getting XML events

4) Use that handler when you call the SAXParser's parse method.



Here is a code example that (I think) does what you want. I didn't test it, so no guarantees. I tried to comment the code, but, this is somewhat complicated in my opinion. So if you have any questions, feel free to leave extra comments.



http://ideone.com/Fh7lP



Edit:



Notice that I extend DefaultHandler, which is a convenience class which basically just wraps the ContentHandler interface (plus some others). This way, instead of implementing a ContentHandler and having to have a bunch of empty methods, I can just extend this and ONLY override the methods I plan on using (it's an adapter class)--which for what you wanted to do was ONLY the startElement method.



Edit:



You can also do XML validation using a SAXParser with a Schema file (it's only slightly more involved).
?
2016-10-13 10:03:35 UTC
i'm not likely to jot down any code simply by fact this looks like a great interest!! yet this is my suggestion... a million) Use the Scanner (java.util) class to envision the document in line by utilising line 2) Use the String.chop up(String regex) technique to chop up each and every line you examine into an array of values (remember to account for areas besides as commas) 3) once you examine the 1st line, use those headings to create a Hashmap (java.util) which will map the string keys you examine in (call, state, area, etc) to the integer indices of those values interior the array.... e.g. key index call -> a million state -> 2 area -> 3 4) this way, once you examine interior the 2d line and chop up it you are able to truly get entry to any key you like - case in point to get the state of a line you have examine and chop up into the String array line[]... String state = line[hashmap.get("state")]; 5) carry out your comparisons from here wish this facilitates :)
Jenny Wilkinson
2012-08-21 02:51:28 UTC
there are plenty of tools out there with built in sax parsers so i would prefer not to build one from scratch, im sure there are a few open source ones as well.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...