Methods Summary |
---|
public static void | attrSearchReplace(org.w3c.dom.Document doc, java.lang.String srchStr, java.lang.String rplStr)Search and replace attributes within the specified XmlDocument
Node m_root = null;
Node child = null;
m_root = doc.getDocumentElement();
child = m_root.getFirstChild();
while (child != null) {
NamedNodeMap attrs = null;
attrs = child.getAttributes();
if (attrs != null)
for (int j=0; j<attrs.getLength(); j++) {
Attr attr = (Attr)attrs.item(j);
if (attr.getName().toLowerCase().indexOf(srchStr) != -1)
attr.setValue(rplStr);
}
child = child.getNextSibling();
}
|
public static void | attrSearchReplace(org.w3c.dom.Node element, java.lang.String srchStr, java.lang.String replaceString)Search and replace attributes within the specified XmlDocument
if (element != null) {
NodeList children = element.getChildNodes();
int noOfChildren = children.getLength();
for (int i = 0; i < noOfChildren; i++) {
Node child = children.item(i);
NamedNodeMap attrs = null;
attrs = child.getAttributes();
if (attrs != null) {
for (int j=0; j<attrs.getLength(); j++) {
Attr attr = (Attr)attrs.item(j);
if (attr.getName().toLowerCase().indexOf(srchStr) != -1)
attr.setValue(replaceString);
}//for
}//if
attrSearchReplace(child,srchStr,replaceString);
}//for
}//if (element != null)
|
public static void | copyXMLFile(org.w3c.dom.Document srcDoc, java.lang.String destFile)Copy the specified Document to the specified file
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(srcDoc);
File destFileObj = new File(destFile);
if(!destFileObj.getParentFile().exists())
destFileObj.getParentFile().mkdirs();
FileWriter destFileWriter = new FileWriter(destFile);
StreamResult result = new StreamResult(destFileWriter);
transformer.transform(source, result);
|
public static void | getAttributes(org.w3c.dom.Node element, java.lang.String srchStr, java.util.List list)
if(element != null && srchStr != null) {
if(list == null)
list = new ArrayList(5);
NodeList children = element.getChildNodes();
int noOfChildren = children.getLength();
for (int i = 0; i < noOfChildren; i++) {
Node child = children.item(i);
String childName = child.getLocalName();
NamedNodeMap attrs = null;
attrs = child.getAttributes();
if (attrs != null) {
for (int j=0; j<attrs.getLength(); j++) {
Attr attr = (Attr)attrs.item(j);
if (attr.getName().toLowerCase().indexOf(srchStr) != -1) {
String attrName =getParentNodeName(child);
list.add( attrName + File.separator + attr);
}
}//for
}//if
getAttributes(child,srchStr,list);
}
}
|
private static java.lang.String | getParentNodeName(org.w3c.dom.Node child)
if(child != null) {
String name = child.getNodeName() ;
//String value = child.getNodeValue();
Node parent = child.getParentNode();
if(parent != null && (parent.getNodeType() == Node.ELEMENT_NODE))
name = getParentNodeName(parent) + File.separator+ name ;
NamedNodeMap attrs = child.getAttributes();
if (attrs != null) {
String nameAttribute = null;
for (int j=0; j<attrs.getLength(); j++) {
Attr attr = (Attr)attrs.item(j);
if(attr.getName().toLowerCase().indexOf("name") != -1) {
nameAttribute = attr.getValue();
continue;
}
}
if (nameAttribute != null)
name = name + "=" + nameAttribute ;
}
return name;
}
return "";
|
public static org.w3c.dom.Document | loadXML(java.lang.String srcXmlFile, java.lang.String dtdFileName)Load an XML file from disk
Document doc = null;
FileInputStream in = null;
in = new FileInputStream(srcXmlFile);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().
newDocumentBuilder();
docBuilder.setEntityResolver(new NOOPHandler(dtdFileName));
doc = docBuilder.parse( in );
if(in!=null)
in.close();
return doc;
|