ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Parsing an XML Document with XPath
Subject:   parsing Weblogic10 domain config file is not working
Date:   2008-05-13 11:52:41
From:   faizy
Hi,


My application deploys in weblogic 10 using an install script. Before deploying the application I validate the domainís config.xml file by parsing it using the XPath expressions. However, the parsing is not working as expected. After trial and error I noticed that in config.xml file if the xmlns attribute of the domain element is pointing to any value (such as xmlns="http://www.bea.com/ns/weblogic/920/domain" or even a space xmlns=" ") then the parsing fails. When I change the attribute to empty value (xmlns="") then parsing works fine. I am not sure why it is failing. Since xmlns attribute is weblogic default and is not in my control to remove or ignore I am not sure how to work around it. I am bound to use DOM parsing instead of XPath even though I really wanted to use XPath expressions to keep the overhead of validation to minimum. Any help in this regard will be greatly appreciated.


I also wanted to know why I have to create new inputSource instance for every xPath.evaluate call. If I donít I get java.io.IOException: Read error - at java.io.FileInputStream.read(Native Method) Ö


Thanks in advance for your help.


The config.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<domain xsi:schemaLocation="http://www.bea.com/ns/weblogic/90/security
http://www.bea.com/ns/weblogic/90/security.xsd
http://www.bea.com/ns/weblogic/90/security/wls
http://www.bea.com/ns/weblogic/90/security/wls.xsd
http://www.bea.com/ns/weblogic/90/security/xacml
http://www.bea.com/ns/weblogic/90/security/xacml.xsd
http://www.bea.com/ns/weblogic/920/domain
http://www.bea.com/ns/weblogic/920/domain.xsd" xmlns="http://www.bea.com/ns/weblogic/920/domain" xmlns:sec="http://www.bea.com/ns/weblogic/90/security" xmlns:wls="http://www.bea.com/ns/weblogic/90/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>testWLS</name>
<domain-version>10.0.1.0</domain-version>
<security-configuration xmlns:xacml="http://www.bea.com/ns/weblogic/90/security/xacml">
<name>testWLS</name>
<realm>
<sec:authentication-provider xsi:type="wls:default-authenticatorType"/>
<sec:authentication-provider xsi:type="wls:default-identity-asserterType">
<sec:active-type>AuthenticatedUser</sec:active-type>
</sec:authentication-provider>
<sec:role-mapper xsi:type="xacml:xacml-role-mapperType"/>
<sec:authorizer xsi:type="xacml:xacml-authorizerType"/>
<sec:adjudicator xsi:type="wls:default-adjudicatorType"/>
<sec:credential-mapper xsi:type="wls:default-credential-mapperType"/>
<sec:cert-path-provider xsi:type="wls:web-logic-cert-path-providerType"/>
<sec:cert-path-builder>WebLogicCertPathProvider</sec:cert-path-builder>
<sec:name>myrealm</sec:name>
</realm>
<default-realm>myrealm</default-realm>
<credential-encrypted>{3DES}hVIDBPyEyDF49ZmYKWvc1vLgvMJsjqZFBpAjxr+DJEqBJMnInWeZVL5/aNXNhIqPRasUcY7IbgSUGFNxIeX+mWY04YP6EiGK</credential-encrypted>
<node-manager-username>weblogic</node-manager-username>
<node-manager-password-encrypted>{3DES}nHo8DzNNGIHSJvD1+Fc0dQ==</node-manager-password-encrypted>
</security-configuration>
<server>
<name>AdminServer</name>
<listen-address/>
</server>
<embedded-ldap>
<name>testWLS</name>
<credential-encrypted>{3DES}NYWWW62LTtiThwuEmGrRiu5AG0eVS/WeKgbk9npcPpc=</credential-encrypted>
</embedded-ldap>
<configuration-version>10.0.1.0</configuration-version>
<admin-server-name>AdminServer</admin-server-name>
</domain>



My XPath parser test class:
package com.test;


import org.xml.sax.InputSource;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import java.io.*;


public class XPathTest {


private static final String fileName = "C:\\config.xml";
private XPathFactory factory = null;
private XPath xPath = null;
private InputSource inputSource = null;


public static void main(String argv[]) {
XPathTest t = new XPathTest();
try {
t.test();
} catch (Exception e) {
e.printStackTrace();
}
}


public XPathTest() {
factory = XPathFactory.newInstance();
xPath = factory.newXPath();
}


private void test() throws Exception {
System.out.println("START > > > > > >");
inputSource = new InputSource(new FileInputStream(new File(this.fileName)));
System.out.println("Version: " + xPath.evaluate("/domain/domain-version", inputSource) );


inputSource = new InputSource(new FileInputStream(new File(this.fileName)));
String expression="/domain/server/name";
NodeList nodeList = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET);
for (int i=0;i Node n = nodeList.item(i);
System.out.println("Node Name: " + n.getNodeName());
System.out.println("Node Value: " + n.getChildNodes().item(0).getNodeValue());
}


System.out.println("END < < < < < <");
}
}