java - 如何使用 Java 从 XML 文件中获取属性
问题描述
我为Oracle Access Manager开发了一个身份验证插件
简而言之,它包含:
- java.class
- 以下XML
我正在尝试从XML 文件中动态获取
<Value>
标签。<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>
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;
}
重要的提示:
该
context
对象是一个包含插件AuthenticationContext
信息的实例
根据 Oracle 的文档,这是有人检索的确切方式,Attribute
但我总是得到NullPointerException
还有其他方法可以检索<Value>
吗?
解决方案
我不得不尝试另一种方式并正确解析XML
如果您可以使用外部库,方法如下:
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();
}
如果你不能
javax
在这里包含库是如何解析XML
使用流
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;
}
}
}
确保你定义tempurlGen
和tempurlVal
推荐阅读
- python - Python 计算由平行线 lat1 和 lat2 以及经线 lon1 和 lon2 界定的表面积
- reactjs - Reactjs - Firebase:取消旧请求
- python - 这个 Youtube API 响应是什么意思?
- python - 无法删除所有未被 5 删除的数字
- c++ - 抽象类,多态 C++ - 在另一个函数中调用纯虚方法
- android - NativeScript / Angular Preview 未在 Android 上重新加载
- lua - fn.id 是什么意思
- python - Python(Selenium)脚本覆盖文件
- python - Psycopg2 上下文管理器提交 SELECT 查询但不提交 UPDATE
- r - 没有找到带有 extrafont 的 GhostScript