如何使用 Java 从 XML 文件中获取属性

我为Oracle Access Manager开发了一个身份验证插件

简而言之,它包含:

  • 以下XML

我正在尝试从XML文件中动态获取<Value>an 的标记 。<Attribute>

<Plugin type="Authentication">

    <author>Phill</author>

    <email>phill@example.com</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>


            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;

            }


重要的提示:


该context对象是AuthenticationContext包含有关插件信息的实例


根据 Oracle 的文档,这是某人检索的确切方式Attribute,但我总是得到NullPointerException


还有其他方法可以检索吗<Value>?


青春有我
浏览 90回答 2
2回答

qq_遁去的一_1

我不得不尝试另一种方法并正确解析XML如果你可以在这里使用外部库是如何:&nbsp; &nbsp; public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; File stocks = new File("PhillPlugin.xml");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Document doc = dBuilder.parse(stocks);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.getDocumentElement().normalize();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList nodes = doc.getElementsByTagName("AttributeValuePair");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < nodes.getLength(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node node = nodes.item(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (node.getNodeType() == Node.ELEMENT_NODE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Element element = (Element) node;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i==0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tempurlGen=getValue("value",element);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("GenerateUrl: " + getValue("value", element));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if (i==1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tempurlVal=getValue("value",element);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("ValidateUrl: " + getValue("value", element));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static String getValue(String tag, Element element) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node node = (Node) nodes.item(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return node.getNodeValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }如果你不能javax在这里包含库是如何解析XML使用流&nbsp; &nbsp; public void getXMLData() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; File stocks = new File("PhillPlugin.xml");&nbsp; &nbsp; &nbsp; &nbsp; BufferedInputStream in = new BufferedInputStream(new FileInputStream(stocks));&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; int cp;&nbsp; &nbsp; &nbsp; &nbsp; while ((cp = in.read()) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append((char) cp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String t = sb.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (t.contains("</AttributeValuePair>")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String test = sb.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String test1p[] = test.split("<value>|</value>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempurlGen = test1p[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; while ((cp = in.read()) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append((char) cp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String t = sb.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (t.contains("</AttributeValuePair>")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String test = sb.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String test1p[] = test.split("<value>|</value>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempurlVal = test1p[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }确保你定义tempurlGen和tempurlVal

人到中年有点甜

尝试这个:@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "", propOrder = {&nbsp; &nbsp; "author",&nbsp; &nbsp; "email",&nbsp; &nbsp; "creationDate",&nbsp; &nbsp; "description",&nbsp; &nbsp; "configuration"})@XmlRootElement(name = "Plugin")public class Plugin {&nbsp; &nbsp; //Add getters and setters including ones for inner classes&nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; private String author;&nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; private String email;&nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; private String creationDate;&nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; private String description;&nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; private Plugin.Configuration configuration;&nbsp; &nbsp; @XmlAttribute(name = "type")&nbsp; &nbsp; private String type;&nbsp; &nbsp; @XmlAccessorType(XmlAccessType.FIELD)&nbsp; &nbsp; @XmlType(name = "", propOrder = {&nbsp; &nbsp; &nbsp; &nbsp; "attributeValuePair"&nbsp; &nbsp; })&nbsp; &nbsp; public static class Configuration {&nbsp; &nbsp; &nbsp; &nbsp; @XmlElement(name = "AttributeValuePair", required = true)&nbsp; &nbsp; &nbsp; &nbsp; private Plugin.Configuration.AttributeValuePair attributeValuePair;&nbsp; &nbsp; &nbsp; &nbsp; @XmlAccessorType(XmlAccessType.FIELD)&nbsp; &nbsp; &nbsp; &nbsp; @XmlType(name = "", propOrder = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "attribute",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mandatory",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "instanceOverride",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "globalUIOverride",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value"&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; public static class AttributeValuePair {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlElement(name = "Attribute", required = true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private Plugin.Configuration.AttributeValuePair.Attribute attribute;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private String mandatory;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private String instanceOverride;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private String globalUIOverride;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private String value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlAccessorType(XmlAccessType.FIELD)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlType(name = "", propOrder = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public static class Attribute {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlValue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private String value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlAttribute(name = "type")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private String type;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @XmlAttribute(name = "length")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private Byte length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}对于解组部分:JAXBContext jaxbContext = JAXBContext.newInstance(Plugin.class);Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();StringReader reader = new StringReader("xml string here");Plugin plugin = (Plugin) unmarshaller.unmarshal(reader);&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java