gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:32:51 38 4
gpt4 key购买 nike

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

简单来说,它包含:

  • java.class
  • 以下XML

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

<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>
<小时/>

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;
}

重要说明:

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

根据 Oracle 的文档,这正是某人检索 Attribute 的方法。但我总是得到 NullPointerException

有没有其他方法可以检索 <Value>

最佳答案

我必须尝试另一种方法并对 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;
}

}
}

确保您定义了 tempurlGentempurlVal

关于java - 如何使用 Java 从 XML 文件获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57196290/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com