import javax.xml.parsers.*;
import org.w3c.dom.*;
public class DOM_sample
{
static String displayText[] = new String[1000];
static int numberLines = 0;
public static void main(String args[])
{
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {}
Document document = null;
document = builder.parse(args[0]);
childLoop(document, "");
} catch (Exception e) {
e.printStackTrace(System.err);
}
for(int loopIndex = 0; loopIndex < numberLines; loopIndex++){
System.out.println(displayText[loopIndex]) ;
}
}
public static void childLoop(Node node, String indentation)
{
if (node == null) {
return;
}
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
displayText[numberLines] = indentation;
displayText[numberLines] +=
"<?xml version=\"1.0\" encoding=\""+
"UTF-8" + "\"?>";
numberLines++;
childLoop(((Document)node).getDocumentElement(), "");
break;
}
case Node.ELEMENT_NODE: {
displayText[numberLines] = indentation;
displayText[numberLines] += "<";
displayText[numberLines] += node.getNodeName();
int length = (node.getAttributes() != null) ?
node.getAttributes().getLength() : 0;
Attr attributes[] = new Attr[length];
for (int loopIndex = 0; loopIndex < length; loopIndex++) {
attributes[loopIndex] =
(Attr)node.getAttributes().item(loopIndex);
}
for (int loopIndex = 0; loopIndex < attributes.length;
loopIndex++) {
Attr attribute = attributes[loopIndex];
displayText[numberLines] += " ";
displayText[numberLines] += attribute.getNodeName();
displayText[numberLines] += "=\"";
displayText[numberLines] += attribute.getNodeValue();
displayText[numberLines] += "\"";
}
displayText[numberLines] += ">";
numberLines++;
NodeList childNodes = node.getChildNodes();
if (childNodes != null) {
length = childNodes.getLength();
indentation += " ";
for (int loopIndex = 0; loopIndex < length; loopIndex++ ) {
childLoop(childNodes.item(loopIndex), indentation);
}
}
break;
}
case Node.TEXT_NODE: {
displayText[numberLines] = indentation;
String trimmedText = node.getNodeValue().trim();
if(trimmedText.indexOf("\n") < 0 && trimmedText.length() > 0){
displayText[numberLines] += trimmedText;
numberLines++;
}
break;
}
case Node.PROCESSING_INSTRUCTION_NODE: {
displayText[numberLines] = indentation;
displayText[numberLines] += "<?";
displayText[numberLines] += node.getNodeName();
String text = node.getNodeValue();
if (text != null && text.length() > 0) {
displayText[numberLines] += text;
}
displayText[numberLines] += "?>";
numberLines++;
break;
}
case Node.CDATA_SECTION_NODE: {
displayText[numberLines] = indentation;
displayText[numberLines] += "<![CDATA[";
displayText[numberLines] += node.getNodeValue();
displayText[numberLines] += "]]>";
numberLines++;
break;
}
}
if (type == Node.ELEMENT_NODE) {
displayText[numberLines] = indentation.substring(0,
indentation.length() - 4);
displayText[numberLines] += "</";
displayText[numberLines] += node.getNodeName();
displayText[numberLines] += ">";
numberLines++;
indentation += " ";
}
}
}
分享到:
相关推荐
一个读取配置文件的工具类 希望可以帮助大家哟
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
[MoonlightService这个string串就是读取配置文件的路径,可以写成宏函数] 3.在判断完m_iniFile是否为空, 接下来就是可以使用了 举例: if (m_iniFile != NULL){ strRet = m_iniFile->GetValue("moonlightService...
java读取配置文件 在经常更换的一些东西 就可以用配置文件
在java类里直接读取配置文件中的信息,用到的方法是this.getClass().getClassLoader().getResourceAsStream
利用java可以读取配置文件信息,可以流来处理这个问题
自己用golang写的路由转发,做了mysql,redis的集成,使用chan做了队列,代码很简单,适合刚刚接触golang,需要用她来做路由转发的同学
通常我们的做法是用配置文件来解决。 各种语言都有自己所支持的配置文件类型。比如Python ,他支持.ini 文件。因为他内部有一个ConfigParser 类来支持.ini 文件的读写,根据该类提供的方法程序员可以自由的来操作....
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
通过按钮,读取配置文件,并修改自定义配置文件appSettings中的值.这是一个很简单的dome.
主要给大家介绍了关于Asp.net Core与类库读取配置文件信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
C#读取配置文件INI 使用的是kernel32实现。
读取和设置配置文件信息
项目中,经常把一部分功能独立出来,做一个java project,然后打成jar包供其他项目引用...这时候,如果jar包中需要读取配置文件信息,则很少把该配置打进jar包,因为它不方便修改,更多都是采用jar包读取外部配置文件。
引入Nuget的两个类库 Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.Json 使用 var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile&#...
工具类里分读取.yml工具类和.properties工具类,结合博客描述使用,用不到的方法可根据个人情况删除,
VC读取可以配置文件是比较简单的,但是有时候需要在配置文件增加一些项目,多数时候需要修改代码,我这个简单的例子实现了增加配置文件,但是不需要修改代码的功能,具体应用的时候可以参考思路。
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
在C#中读写INI配置文件 个人珍藏版一般人我可不让他下载哦 大家赶紧下吧
最近写一个小程序,需要读取保存在配置文件中的配置信息。 这是一个简单的例子,可以以这个程序为基础写一个完整的类出来。