Phill Alexakis
Phill Alexakis

Reputation: 1499

How to get an attribute from XML file using Java

I have developed an Authentication Plugin for Oracle Access Manager

Briefly it contains:

I'm trying to Dynamically get the <Value> tag of an <Attribute> from the XML file.

<Plugin type="Authentication">
    <author>Phill</author>
    <email>[email protected]</email>
    <creationDate>12:47:00, 2019-07-11</creationDate>
    <description>Phill-Plugin</description>
    <configuration>
        <AttributeValuePair>
            <Attribute type="string" length="60">GenerateUrl</Attribute>
            <mandatory>true</mandatory>
            <instanceOverride>false</instanceOverride>
            <globalUIOverride>true</globalUIOverride>
            <value>This is the value i'm trying to retrieve</value>
        </AttributeValuePair>
    </configuration>
</Plugin>

java.class

            try {

                CredentialParam tem = context.getCredential().getParam("GenerateUrl");
                String temp = (String) tem.getValue();
                System.out.println("TEST: " + temp);
                generateUrl = temp + "The User" + user;
            } catch (Exception e) {
                System.out.println("\n\n\n-------------------\n");
                System.out.println("-      Input Is:         -\n");
                System.out.println("-       "+e+"            -\n");
                System.out.println("-------------------\n");
                generateUrl = "A URL" + "The User" + user;
            }

Important Note:

The context object is an AuthenticationContext instance containing information about the Plug-in

Based on Oracle's Documentation, this is the exact way someone retrieves an Attribute but i'm always getting a NullPointerException

Is there any other way that i can retrieve the <Value>?

Upvotes: 1

Views: 1433

Answers (2)

Phill Alexakis
Phill Alexakis

Reputation: 1499

I had to try another way and do proper parsing of the XML

If you can use external libs here is how:


    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        File stocks = new File("PhillPlugin.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(stocks);
            doc.getDocumentElement().normalize();

            NodeList nodes = doc.getElementsByTagName("AttributeValuePair");

            for (int i = 0; i < nodes.getLength(); i++) {
              Node node = nodes.item(i);
              if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if(i==0)
                 {
                 tempurlGen=getValue("value",element);
                   System.out.println("GenerateUrl: " + getValue("value", element));
                 }
                 else if (i==1)
                 {
                 tempurlVal=getValue("value",element);
                 System.out.println("ValidateUrl: " + getValue("value", element));
                 }

              }
            }
          }
          static String getValue(String tag, Element element) {
            NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
            Node node = (Node) nodes.item(0);
            return node.getNodeValue();
          }

If you can't include javax libraries here is how to parse the XML using streams


    public void getXMLData() throws Exception {
        File stocks = new File("PhillPlugin.xml");
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(stocks));
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = in.read()) != -1) {
            sb.append((char) cp);
            String t = sb.toString();
            if (t.contains("</AttributeValuePair>")) {
                String test = sb.toString();
                String test1p[] = test.split("<value>|</value>");
                tempurlGen = test1p[1];
                break;
            }
        }

        sb = new StringBuilder();
        while ((cp = in.read()) != -1) {

            sb.append((char) cp);
            String t = sb.toString();
            if (t.contains("</AttributeValuePair>")) {
                String test = sb.toString();
                String test1p[] = test.split("<value>|</value>");
                tempurlVal = test1p[1];
                break;
            }

        }
    }

Make sure you define tempurlGen and tempurlVal

Upvotes: 1

sechanakira
sechanakira

Reputation: 253

Try this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "author",
    "email",
    "creationDate",
    "description",
    "configuration"
})
@XmlRootElement(name = "Plugin")
public class Plugin {

    //Add getters and setters including ones for inner classes

    @XmlElement(required = true)
    private String author;
    @XmlElement(required = true)
    private String email;
    @XmlElement(required = true)
    private String creationDate;
    @XmlElement(required = true)
    private String description;
    @XmlElement(required = true)
    private Plugin.Configuration configuration;
    @XmlAttribute(name = "type")
    private String type;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "attributeValuePair"
    })
    public static class Configuration {

        @XmlElement(name = "AttributeValuePair", required = true)
        private Plugin.Configuration.AttributeValuePair attributeValuePair;

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "attribute",
            "mandatory",
            "instanceOverride",
            "globalUIOverride",
            "value"
        })
        public static class AttributeValuePair {

            @XmlElement(name = "Attribute", required = true)
            private Plugin.Configuration.AttributeValuePair.Attribute attribute;
            @XmlElement(required = true)
            private String mandatory;
            @XmlElement(required = true)
            private String instanceOverride;
            @XmlElement(required = true)
            private String globalUIOverride;
            @XmlElement(required = true)
            private String value;

            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = {
                "value"
            })
            public static class Attribute {

                @XmlValue
                private String value;
                @XmlAttribute(name = "type")
                private String type;
                @XmlAttribute(name = "length")
                private Byte length;

            }

        }

    }

}

And for the unmarshaling part:

JAXBContext jaxbContext = JAXBContext.newInstance(Plugin.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("xml string here");
Plugin plugin = (Plugin) unmarshaller.unmarshal(reader); 

Upvotes: 0

Related Questions