-2

I'm having a simple xml string as

<Detail>
    <firstname xmlns=""></firstname >
    <lastname xmlns=""></lastname>               
</Detail>

If there is no xmlns="", I'm able to get the value, but when xmlns attribute comes, its not returning the value

Here is my code

getTagValue(strResponse, "firstname ")

public static String getTagValue(String xml, String tagName){
    return xml.split("<"+tagName+">")[1].split("</"+tagName+">")[0];
}

How can I get firstname value. I dont want to use DocumentBuilder classes for this single element

Aliy
  • 197
  • 14
  • 3
    Did you try using parsers instead of reinventing the wheel? – BackSlash Sep 27 '17 at 09:09
  • Damn, why are u splitting it? Why don't you parse xml and get nodes,elements and tag names – Optional Sep 27 '17 at 09:12
  • Using text operations on XML strings is a much more complicated thing than it first seems to be. There could be XML comments that you want to drop. There might be more whitespace inside the element declarations (inside the angle brackets). And much more. Do yourself a favor and use one of the many parsers that exist for that purpose. – Seelenvirtuose Sep 27 '17 at 09:12
  • @Optional, I can get the value using Parser. But instead of that is there any other way in a simple way? – Aliy Sep 27 '17 at 09:12
  • 1
    @Aliy Using a parser is the simplest way. If you do this by hand, you'll need to basically write your own parser, which is both more time consuming and more complex than using an already existing parser. – BackSlash Sep 27 '17 at 09:14
  • you could refer https://stackoverflow.com/questions/5059224/which-is-the-best-library-for-xml-parsing-in-java – Ravi Sep 27 '17 at 09:15
  • If you want to read the entire XML and build up an object hierarchy from the read values, then use a parser. SAX, StAX, DOM, and many others exist. If you want to extract some values, you could also use XPath. Java has many options built in. Apart from that: Your question is too broad. Voting to close ... – Seelenvirtuose Sep 27 '17 at 09:16

1 Answers1

0

I think the easiest way, without explicitly parsing the XML text, would be using JAXB as follows. Create a Detail class that only has annotated the variables that you want to load from the input XML.

For an input.xml file, which not only contains firstname and lastname, but also other XML elements (as requested in the comments):

<?xml version="1.0" encoding="UTF-8"?>
<Detail>
    <firstname>firstname</firstname>
    <lastname>lastname</lastname>               
    <elementA>element A</elementA>
    <elementB>element B</elementB>
    <elementC>element C</elementC>
</Detail>

If you have a Detail class with only firstname and lastname annotated as follows:

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "Detail")
@XmlType(propOrder = { "firstname", "lastname"})
public class Detail {

    @XmlElement(name="firstname")
    private String firstname;

    @XmlElement(name="lastname")
    private String lastname;

    private String elementA = "default elementA";

    private String elementB = "default elementB";

    private String elementC = "default elementC";

    public Detail(){}

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getElementA() {
        return elementA;
    }

    public void setElementA(String elementA) {
        this.elementA = elementA;
    }

    public String getElementB() {
        return elementB;
    }

    public void setElementB(String elementB) {
        this.elementB = elementB;
    }

    public String getElementC() {
        return elementC;
    }

    public void setElementC(String elementC) {
        this.elementC = elementC;
    }
}

and also a jaxb.index file, located in the same folder as the compiled classes, whose only content is:

Detail

and you test your input XML like this:

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Test {

    public static void main(String[] args) {
        try{
            JAXBContext jc = JAXBContext.newInstance(Detail.class);

            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("input.xml");
            Detail detail = (Detail) unmarshaller.unmarshal(xml);
            System.out.println("Detail firstname:"+detail.getFirstname()+" lastname:"+detail.getLastname());
        }
        catch(JAXBException e){
            System.out.println(e.getMessage());
        }
    }
}

You will see that Detail has only firstname and lastname loaded from the XML file.

Guillem
  • 456
  • 4
  • 9
  • @Gulliem, what will happen if my Detail XML object has 100 tags? If I want to just retrieve firstname & lastname, can I still have the class only with two variables? – Aliy Sep 28 '17 at 05:06
  • @Aliy Any XML element that is not annotated in the Detail class will not be loaded from the XML file, have a look at the edited reply and try yourself. Also, previously forgot to say that there must be a file named jaxb.index inside the same folder as the compiled classes, whose content has to be "Detail". This file lists which classes are annotated, otherwise JAXB will throw an exception. – Guillem Sep 28 '17 at 09:27
  • can you please check this question. https://stackoverflow.com/questions/46462194/not-able-to-extract-only-selected-tag-values-using-jaxb-in-java – Aliy Sep 28 '17 at 09:34
  • @Aliy if my answer solved your question, please mark it as solved – Guillem Sep 28 '17 at 10:18
  • Will accept it. Did you have a look on the above question? I have that question – Aliy Sep 28 '17 at 10:39