哈工大cerp系统代码
源代码在线查看: xmlparser.java
/*
* $Id: XMLParser.java,v 1.1.1.1 2002/02/02 05:20:16 Administrator Exp $
* Copyright 2001 Sun Microsystems, Inc. All rights reserved.
* Copyright 2001 Sun Microsystems, Inc. Tous droits r閟erv閟.
*/
package com.huiton.cerp.pub.util;
import org.xml.sax.InputSource;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
// jaxp 1.0.1 imports
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import com.huiton.mainframe.util.tracer.Debug;
public class XMLParser {
// constants
protected static Element loadDocument(String location) {
Document doc = null;
try {
URL url = new URL(location);
InputSource xmlInp = new InputSource(url.openStream());
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
doc = parser.parse(xmlInp);
Element root = doc.getDocumentElement();
//root.normalize();
return root;
} catch (SAXParseException err) {
Debug.println ("XMLParser ** Parsing error" + ", line " +
err.getLineNumber () + ", uri " + err.getSystemId ());
Debug.println("XMLParser error: " + err.getMessage ());
} catch (SAXException e) {
Debug.println("XMLParser error: " + e);
} catch (java.net.MalformedURLException mfx) {
Debug.println("XMLParser error: " + mfx);
} catch (java.io.IOException e) {
Debug.println("XMLParser error: " + e);
} catch (Exception pce) {
Debug.println("XMLParser error: " + pce);
}
return null;
}
private static String getSubTagAttribute(Element root, String tagName, String subTagName, String attribute) {
String returnString = "";
NodeList list = root.getElementsByTagName(tagName);
for (int loop = 0; loop < list.getLength(); loop++) {
Node node = list.item(loop);
if (node != null) {
NodeList children = node.getChildNodes();
for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
Node child = children.item(innerLoop);
if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
if (child instanceof Element) {
return ((Element)child).getAttribute(attribute);
}
}
} // end inner loop
}
}
return returnString;
}
protected static String getSubTagValue(Node node, String subTagName) {
String returnString = "";
if (node != null) {
NodeList children = node.getChildNodes();
for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
Node child = children.item(innerLoop);
if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
Node grandChild = child.getFirstChild();
if (grandChild.getNodeValue() != null) return grandChild.getNodeValue();
}
} // end inner loop
}
return returnString;
}
protected static String getSubTagValue(Element root, String tagName, String subTagName) {
String returnString = "";
NodeList list = root.getElementsByTagName(tagName);
for (int loop = 0; loop < list.getLength(); loop++) {
Node node = list.item(loop);
if (node != null) {
NodeList children = node.getChildNodes();
for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
Node child = children.item(innerLoop);
if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
Node grandChild = child.getFirstChild();
if (grandChild.getNodeValue() != null) return grandChild.getNodeValue();
}
} // end inner loop
}
}
return returnString;
}
protected static String getTagValue(Element root, String tagName) {
String returnString = "";
System.out.println("-----tagName: " + tagName);
NodeList list = root.getElementsByTagName(tagName);
for (int loop = 0; loop < list.getLength(); loop++) {
Node node = list.item(loop);
if (node != null) {
Node child = node.getFirstChild();
if ((child != null) && child.getNodeValue() != null) {
System.out.println("-----returnString: " + child.getNodeValue());
return child.getNodeValue();
}
}
}
return returnString;
}
}