0

I have to read line by line an Xml file in java.

The file has lines of the format :

    <CallInt xsi:type="xsd:int">124</CallInt>

I need to pick up only tag name CallInt and the value 124 from the above line. I tried using String Tokenizer, Split etc. But nothing to the rescue.

Can anyone help me with this?

Some code

    BufferedReader buf = new BufferedReader(new FileReader(myxmlfile));

    while((line = buf.readLine())!=null)
    {
    String s = line;
    // Scanning for the tag and the integer value code???
    }
  • 1
    Star by having a read through. [Trail: Java API for XML Processing (JAXP)](http://docs.oracle.com/javase/tutorial/jaxp/). Personally, I'd read the file into a DOM and then use XPath to query it. But if your only after a single line, SAX parsing might be more efficient – MadProgrammer Nov 08 '13 at 21:35
  • Why do you have to read it line by line? That is completely the wrong way to read XML, it totally misses the point of XML, which is not a line-based format. CSV is a line-based format, XML isn't. – Robin Green Nov 08 '13 at 21:38
  • I have had a look at that. But I don't have to use them. I have to purely code in Java. Do you know how could that be done? – user2970551 Nov 08 '13 at 21:39
  • @RobinGreen : I have to further invoke method with that tag name for a specific object.So I need to read it line by line – user2970551 Nov 08 '13 at 21:41
  • No you don't. You can read it element by element. (That's what SAX is for.) The fact that you may happen to have one element alone on most lines does not mean you need to read line by line. The top level element is not a single line, it spans the whole file. – Robin Green Nov 08 '13 at 21:46
  • Whoever is asking you to code it purely in Java better have a really good reason, or they are mis-educating you. – Robin Green Nov 08 '13 at 21:48

2 Answers2

0

You should really use a small xml parser.

If you have to read line-by-line, and the format is guaranteed to be line-based, search for delimiters around the content you want to extract with indexOf() and then use substring()...

int cut0 = line.indexOf('<');
if (cut0 != -1) {
  int cut1 = line.indexOf(' ', cut0);
  if (cut1 != -1) {
    String tagName = line.substring(cut0 + 1, cut1);

    int cut2 = line.indexOf('>', cut1);  // insert more ifs as needed...
    int cut3 = line.indexOf('<', cut2);

    String value = line.substring(cut2 + 1, cut2);
  }
}
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
0

Here's a small example with StaX.

Note I've removed the reference to the schema for simplicity (it'll fail as is otherwise).

XML file called "test", in path "/your/path"

<thingies>
    <thingie foo="blah"/>
    <CallInt>124</CallInt>
</thingies>

Code

XMLInputFactory factory = null;
XMLStreamReader reader = null;
// code is Java 6 style, no try with resources
try {
    factory = XMLInputFactory.newInstance();
    // coalesces all characters in one event
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    reader = factory.createXMLStreamReader(new FileInputStream(new File(
            "/your/path/test.xml")));
    boolean readCharacters = false;
    while (reader.hasNext()) {
        int event = reader.next();
        switch (event) {
        case (XMLStreamConstants.START_ELEMENT): {
            if (reader.getLocalName().equals("CallInt")) {
                readCharacters = true;
            }
            break;
        }
        case (XMLStreamConstants.CHARACTERS): {
            if (readCharacters) {
                System.out.println(reader.getText());
                readCharacters = false;
            }
            break;
        }
        }
    }
}
catch (Throwable t) {
    t.printStackTrace();
}
finally {
    try {
        reader.close();
    }
    catch (Throwable t) {
        t.printStackTrace();
    }
}

Output

124

Here is an interesting SO thread on schemas and StaX.

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106