快捷搜索:

java xml 冒号_xml文件中字符串带有冒号通过java怎么解析?

这个问题是考察api的,只要认真按doc来查找相对应的java方法没什么难度,希望以后能自己解决。package com.xml.resolver;

import java.io.File;

import java.io.IOException;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.io.FileUtils;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;

public class Main {

public static void main(String[] args) throws IOException {

String xml = FileUtils.readFileToString(new File("c:/a.xml"));

System.out.println(xml);

try {

DocumentBuilderFactory factory = DocumentBuilderFactory

.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder

.parse(new InputSource(new StringReader(xml)));

Element root = doc.getDocumentElement();

NodeList nodeList = root

.getElementsByTagName("cas:authenticationSuccess");

if (nodeList != null) {

for (int i = 0; i < nodeList.getLength(); i++) {

Node book = nodeList.item(i);

NodeList _nodeList = book.getChildNodes();

for (int j = 0; j < _nodeList.getLength(); j++) {

Node _book = nodeList.item(i);

System.out.println("节点=" + _book.getNodeName()

+ " text=" + _book.getTextContent());

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

输出:

这个问题是考察api的,只要认真按doc来查找相对应的java方法没什么难度,希望以后能自己解决。package com.xml.resolver; import java.io.File; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.io.FileUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class Main { public static void main(String[] args) throws IOException { String xml = FileUtils.readFileToString(new File("c:/a.xml")); System.out.println(xml); try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder .parse(new InputSource(new StringReader(xml))); Element root = doc.getDocumentElement(); NodeList nodeList = root .getElementsByTagName("cas:authenticationSuccess"); if (nodeList != null) { for (int i = 0; i < nodeList.getLength(); i++) { Node book = nodeList.item(i); NodeList _nodeList = book.getChildNodes(); for (int j = 0; j < _nodeList.getLength(); j++) { Node _book = nodeList.item(i); System.out.println("节点=" + _book.getNodeName() + " text=" + _book.getTextContent()); } } } } catch (Exception e) { e.printStackTrace(); } } } 输出:
经验分享 程序员 微信小程序 职场和发展