tags:

views:

176

answers:

10

Could somebody help me with this. I would like to know how to read this example as string? I know how to read first one but don't know how to read them all

<Tr rn=\"000000000000000\" vr=\"T\" sSpre=\"S\" reg=\"P\" dSpre=\"2000-01-01\" dOdprt=\"2000-01-01\" iban=\"SI00\" eno=\"R\" vir=\"B\" maticnaPps=\"0000000000\"><Imetnik davcna=\"00000000\" matSub=\"0000000000\" drz=\"705\"><PopolnoIme>UNKNOWN</PopolnoIme><KratkoIme>UNKNOWN</KratkoIme><Naslov sifTipNaslova=\"00\" sifObcina=\"000\" sifPosta=\"0000\" sifUlica=\"0000\" sifNaselje=\"000\" stHisna=\"000\" sifHsmid=\"00000000\"><Obcina>UNKNOWN</Obcina><Posta>UNKNOWN</Posta><Ulica>UNKNOWN</Ulica><Naselje>UNKNOWN</Naselje></Naslov></Imetnik></Tr>
A: 

The best solution to parse XML files in Java is to use a dedicated library such as:

  1. Xerces
  2. Sax
Manuel Selva
I put \" so that java doesn't count it as comment. Yes, I want to read this line as string.
Igor
+2  A: 

Maybe this is what you are looking for? Example here: http://ideone.com/N4jIO

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Main {

    public static void main(String... args) throws IOException, SAXException, ParserConfigurationException {

        String xml = "<Tr rn=\"000000000000000\" vr=\"T\" sSpre=\"S\" reg=\"P\" dSpre=\"2000-01-01\" dOdprt=\"2000-01-01\" iban=\"SI00\" eno=\"R\" vir=\"B\" maticnaPps=\"0000000000\"><Imetnik davcna=\"00000000\" matSub=\"0000000000\" drz=\"705\"><PopolnoIme>UNKNOWN</PopolnoIme><KratkoIme>UNKNOWN</KratkoIme><Naslov sifTipNaslova=\"00\" sifObcina=\"000\" sifPosta=\"0000\" sifUlica=\"0000\" sifNaselje=\"000\" stHisna=\"000\" sifHsmid=\"00000000\"><Obcina>UNKNOWN</Obcina><Posta>UNKNOWN</Posta><Ulica>UNKNOWN</Ulica><Naselje>UNKNOWN</Naselje></Naslov></Imetnik></Tr>";

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

        print(doc.getDocumentElement(), "");
    }

    private static void print(Node e, String tab) {

        if (e.getNodeType() == Node.TEXT_NODE) {
            System.out.println(tab + e.getNodeValue());
            return;
        }


        System.out.print(tab + e.getNodeName());

        NamedNodeMap as = e.getAttributes();
        if (as != null && as.getLength() > 0) {
            System.out.print(" attributes=[");
            for (int i = 0; i < as.getLength(); i++) 
                System.out.print((i == 0 ? "" : ", ") + as.item(i));
            System.out.print("]");
        }
        System.out.println();

        if (e.getNodeValue() != null)
            System.out.println(tab + " " + e.getNodeValue());

        NodeList childs = e.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++)
            print(childs.item(i), tab + " ");
    }
}
dacwe
Pretty close, yes. I'll try to copy/paste output from my scratch to see in what format output should be. rn = 031001002126306
Igor
I just don't know how to roll my scratch to read whole content from line.
Igor
A: 

I like Jakarta Commons IO to read text files:

String text = FileUtils.readFileToString(file, "UTF-8");
Thilo
A: 

If your goal is to load/parse an XML Document from a String object, you'll simply need to use the usual XML document loading code, but to use a StringReader to provide your inputstream. (or a ByteArrayInputStream, or anything really as long as you build up a chain of transformations that lets your access your data as an InputStream).

An example follows here (untested and without exception handling. Sorry, I don't have a test environment at the moment):

  final DocumentBuilderFactory f  = DocumentBuilderFactory.newInstance();
  final DocumentBuilder        db = f.newDocumentBuilder();
  final InputSource            is = new InputSource();

  is.setCharacterStream(new StringReader(YOURSTRING));
  final Document               doc = db.parse(is);

  doc.getDocumentElement().normalize();
  /*
   * do whatever you want/need here.
   */

If that's not what you wanted, sorry I am not quite sure what you were asking here.

haylem
A: 

This is my scratch but don't know how to continue:

public class ParseLine {
public static void Parse (String myLine) {
        String myWorkLine = null;
        String myTag = null;
        String myValue = null;
        if (myLine.substring(0,4).equals("<Tr ")) {
            myWorkLine = myLine.substring(4);
                myTag = myWorkLine.substring(0,myWorkLine.indexOf("="));
                myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2);
                myValue = myWorkLine.substring(0,myWorkLine.indexOf("\""));
                myWorkLine = myWorkLine.substring(myWorkLine.indexOf("\"")+2);
                System.out.println(myTag + " " + myValue);
            }
    else
        System.out.println("ERROR, line doesn't start properly");
        //System.exit(-1);
    }
}
Igor
A: 

Using xerces could be more understandable:

public static void loadImetniks(String filePath) {

        File xmlFile;

        SAXBuilder builder;

        Element root, child;

        Imetnik imet;//another class that you have to create to help you for parsing

        Document doc;

        try {

            xmlFile = new File(filePath);

            builder = new SAXBuilder();  // parameters control validation, etc

            doc = builder.build(xmlFile);



            root = doc.getRootElement(); // Tr could be the root but I am not sure if you will have more Tr nodes in the same file?? 

            tr.setRn(root.getAttributeValue(Constants.RN));//define the constants string in another file
            tr.setVr(root.getAttributeValue(Constants.VR));
            tr.setSspre(root.getAttributeValue(Constants.SSPRE));
            tr.setReg(root.getAttributeValue(Constants.REG));
            tr.setIban(root.getAttributeValue(Constants.IBAN));
        .... //repeat for every attribute
        ....



            List children = root.getChildren(); // depends of how many Imetnik you will have

            for (Iterator iter = children.iterator(); iter.hasNext();) {

                child = (Element) iter.next();

                imet = new Imetnik();

                imet.loadXML(child); // you have to define the loadXML function in your object Imetnik which should extract the attributes and internal nodes

                //imets.add(contest); // just use in the case that you will have to extract more than one Imetnik node

            }



        } catch (Exception e) {

            log.error("Error al hacer el parsing del contests.xml!");

            log.error(e.getMessage());

        }

    }

For instance, your Imetnik class should contain:

   public void loadXML(Element root) {

        Element child;

    //Naslov naslov;  // for Naslov because it could be an object itself


        davcna = root.getAttributeValue(Constants.DAVCNA); //define the string constant
        matSub = root.getAttributeValue(Constants.MATSUB); //define the string constant
        drz = Integer.parseInt(root.getAttributeValue(Constants.DRZ)); //define the string constant



        List children = root.getChildren(); // your root is Imetnik now

        for (Iterator iter = children.iterator(); iter.hasNext();) {
          .....
          .......
       }
}
Nervo Verdezoto
A: 

Thank you for all answers. But I think I must do it with something like I posted. I'm novice in java and my boss gave me scratch like this and I think he wants me to continue in this way.
Does anybody know how to continue this code? Basically, I just don't know how to make loop to check all strings.

public class ParseLine {
public static void Parse (String myLine) {
        String myWorkLine = null;
        String myTag = null;
        String myValue = null;
        if (myLine.substring(0,4).equals("<Tr ")) {
            myWorkLine = myLine.substring(4);
                while ("SOMETHING") {
                myTag = myWorkLine.substring(0,myWorkLine.indexOf("="));
                myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2);
                myValue = myWorkLine.substring(0,myWorkLine.indexOf("\""));
                myWorkLine = myWorkLine.substring(myWorkLine.indexOf("\"")+2);
                System.out.println(myTag + " " + myValue);
                } 
         }
    else
        System.out.println("ERROR, line doesn't start properly");
        //System.exit(-1);
    }
}
Igor
Anybody, please?
Igor
A: 

Hi!

Could someone tell me how to extract bla bla with my sample code?

<FullName>blabla</FullName>

private static void PreveriVred(String myLine1){
            String myWorkLine1 = null;
            String myTag1 = null;
            String myValue1 = null;

            if (myLine1.substring(0,4).equals("<Tr ")) {
            myWorkLine1 = myLine1.substring(4);
            while (!myLine1.substring(0,myLine1.length()).equals("<")) {
                myTag1 = myWorkLine1.substring(0,myWorkLine1.indexOf("="));
                myWorkLine1 = myWorkLine1.substring(myWorkLine1.indexOf("=")+2);
                myValue1 = myWorkLine1.substring(0,myWorkLine1.indexOf("\""));
                myWorkLine1 = myWorkLine1.substring(myWorkLine1.indexOf("\"")+2);
                System.out.println(myTag1 + "  " + myValue1 + "  " + myWorkLine1);
                }
            }
    }
Igor
A: 

Hi!

I solved some problems but there are new ones now and I don't know how to continue. Any help would be much appreciated

package javaapplication2;

public class ParseLine{
public static void Parse (String myLine) throws Exception {
        String myWorkLine = null;
        String myTag = null;
        String myValue = null;
        int i=0;

        if (myLine.substring(0,4).equals("<Tr ")) {
            for (;i<myLine.length();i++) {
            myWorkLine = myLine.substring(4);
            while (!myLine.substring(0,i).equals("</Tr>")) {
                myTag = myWorkLine.substring(0,myWorkLine.indexOf("="));
                myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2);
                myValue = myWorkLine.substring(0,myWorkLine.indexOf("\""));
                myWorkLine = myWorkLine.substring(myWorkLine.indexOf("\"")+2);
                System.out.println(myTag + " " + myValue); 

                if (myWorkLine.substring(0,1).equals("<")) {
                    myWorkLine = myWorkLine.substring(1);
                    while (!myLine.substring(0, i).equals("<")) {
                        myTag = myWorkLine.substring(0,myWorkLine.indexOf(("=")));
                        myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2);
                        myValue=myWorkLine.substring(0,myWorkLine.indexOf(("\"")));
                        myWorkLine=myWorkLine.substring(myWorkLine.indexOf("\"")+2);
                        System.out.println(myTag + " " + myValue); 

                        if (myWorkLine.substring(0,2).equals("<")) {
                            myWorkLine = myWorkLine.substring(2);
                            while (!myLine.substring(0,i).equals(">")) {
                                myTag = myWorkLine.substring(0,myWorkLine.indexOf("<"));
                                myWorkLine = myWorkLine.substring(myWorkLine.indexOf("<")+1);
                                myValue = myWorkLine.substring(0,myWorkLine.indexOf(">"));
                                myWorkLine = myWorkLine.substring(myWorkLine.indexOf(">")+1);
                                System.out.println(myTag + " " + myValue);

                            } //3. while                                                 
                        }//3. if 

                    } //2. while 
                } //2. if 

            } //1. while 
           } //for 
        } else //if 
        System.out.println("ERROR! Line doesn't start with <Tr ");


   } //Parse 
} //ParseLine 

and this is my output. I think you can see what is wrong. Please, help. :)

rn 000000000000000 vr T sSpre S reg P dSpre 2010-01-01 dOdprt 2007-01-01 iban SI00 vir R maticnaPps 00000000000 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 PopolnoImeRacuna>TEXT</PopolnoImeRacuna><KratkoImeRacuna>TEXT</KratkoImeRacuna><NazivPp>TEXT</NazivPp><Imetnik davcna 00000001 matSub 0000000000 drz 705 <PopolnoIme>TEXT</PopolnoIme><KratkoIme>TEXT</KratkoIme><Naslov sifTipNaslova 01 sifObcina 000 sifPosta 0000 sifUlica 0000 sifNaselje 000 stHisna 000 sifHsmid 00000000
Igor