Reading XML as string in Java

15,386

Solution 1

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 + " ");
    }
}

Solution 2

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.

Share:
15,386
Igor
Author by

Igor

Updated on June 04, 2022

Comments

  • Igor
    Igor almost 2 years

    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>
    
  • Igor
    Igor over 13 years
    I put \" so that java doesn't count it as comment. Yes, I want to read this line as string.
  • Igor
    Igor over 13 years
    Pretty close, yes. I'll try to copy/paste output from my scratch to see in what format output should be. rn = 031001002126306
  • Igor
    Igor over 13 years
    I just don't know how to roll my scratch to read whole content from line.
  • james.garriss
    james.garriss almost 9 years
    Not a very helpful answer. Perhaps you provide a bit of code to get the OP going. Or point to a good tutorial.