-2

I have an string which I am attempting to extract values from, for convenience I thought that converting the string to a Document and then parsing the xml would be the best way to do this but I am running into all sorts of problems! The string looks like:

    <Messagexxx>
             <Unit> 
                <contact>0</contact> 
                <text>Test Content</text>
                <date>09-Sep-14 13:56</date>
                <subject>Test Title</subject>
             </Unit>
     </Messagexxx>

Can someone point me in the correct way to achieve my goal of reading the values from the tags .

I have attempted using the following snippet but I all the values in the array are

null! Document xml = null; Node T = null; try { xml = stringToDom(message); T = xml.getLastChild(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(xml.getFirstChild() != null){ }

JavaBeigner
  • 608
  • 9
  • 28
joebohen
  • 145
  • 1
  • 14
  • 5
    What are "all kinds of problems"? Show the code you use and describe the issues you have with it. – Tomalak Sep 09 '14 at 13:20
  • Search for SAX and DOM – Marco Acierno Sep 09 '14 at 13:22
  • Hi I have attempted using the following snippet but I all the values in the array are null! Document xml = null; Node T = null; try { xml = stringToDom(message); T = xml.getLastChild(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(xml.getFirstChild() != null){ } – joebohen Sep 09 '14 at 13:27
  • 3
    You shouldn't important code in comments because (1) it is unreadable (2) not everyone is reading comments (so you are lowering your chances of getting answer). So instead of posting it as comment use [[edit]] option below your question and include it in your post. – Pshemo Sep 09 '14 at 13:34
  • Duplicate - http://stackoverflow.com/questions/562160/in-java-how-do-i-parse-xml-as-a-string-instead-of-a-file – Nick Holt Sep 09 '14 at 14:40
  • Yes Sorry about the duplicate which I created in error. – joebohen Sep 09 '14 at 15:36

3 Answers3

0

When you write your string to a text file, you can first parse it:

private Document parse(String filename){
    Document doc = null;
    try {
        DOMParser parser = new DOMParser();
        parser.parse(filename);
        doc = parser.getDocument();     
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return doc;
}

and then you read all text elements out of this document:

public void extract (Document doc){
    Node root = doc.getDocumentElement();
    for (int i = 0; i< root .getChildNodes().getLength(); i++){
      Node child = root.getChildNodes().item(i);
      System.out.println(child.getTextContent());
    }
}
  • Why on earth would you write the XML to a file when you can use `DOMParser.parse(Reader)` and pass a `StringReader`? – Nick Holt Sep 09 '14 at 14:39
0

Use JAXB lib : https://jaxb.java.net/

Create your model from your XML and to read :

JAXBContext jaxbContext = JAXBContext.newInstance(YourModel.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader("xml string here");
YourModel yourModel= (Person) unmarshaller.unmarshal(reader);

After your can use the object "YourModel" to read your value.

stacky
  • 800
  • 6
  • 18
  • Thanks for your comments I agree that it looks a little long winded to save the existing string to an xml file then reload it, but I can find no documentation to add JAXB to an existing project! – joebohen Sep 09 '14 at 15:39
  • You have an example with source here : http://www.java2blog.com/2013/02/jaxb-tutorial.html – stacky Sep 09 '14 at 15:45
0

This is a very simple way to get node values when you know the node names, and they don't repeat:

String getXmlNodeValue(String xmlString, String nodeName){
    int start = xmlString.indexOf("<"+nodeName+">") + nodeName.length() + 2;
    int end = xmlString.indexOf("</"+nodeName+">");
    return xmlString.substring(start, end);
}
Andres
  • 3
  • 2