java xml node_从xml node – java获取行号

按照这个例子我已经得到了这个工作:

该解决方案遵循Michael Kay建议的方法。这是你如何使用它:

// XmlTest.java

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class XmlTest {

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

String xmlString = "\n"

+ " \n"

+ " Hello World!\n"

+ " \n"

+ "";

InputStream is = new ByteArrayInputStream(xmlString.getBytes());

Document doc = PositionalXMLReader.readXML(is);

is.close();

Node node = doc.getElementsByTagName("moo").item(0);

System.out.println("Line number: " + node.getUserData("lineNumber"));

}

}

如果你运行这个程序,它会输出:“行号:3”

PositionalXMLReader是上面链接的一个稍微修改版本。

// PositionalXMLReader.java

import java.io.IOException;

import java.io.InputStream;

import java.util.Stack;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.xml.sax.Attributes;

import org.xml.sax.Locator;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class PositionalXMLReader {

final static String LINE_NUMBER_KEY_NAME = "lineNumber";

public static Document readXML(final InputStream is) throws IOException, SAXException {

final Document doc;

SAXParser parser;

try {

final SAXParserFactory factory = SAXParserFactory.newInstance();

parser = factory.newSAXParser();

final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

doc = docBuilder.newDocument();

} catch (final ParserConfigurationException e) {

throw new RuntimeException("Can't create SAX parser / DOM builder.", e);

}

final Stack elementStack = new Stack();

final StringBuilder textBuffer = new StringBuilder();

final DefaultHandler handler = new DefaultHandler() {

private Locator locator;

@Override

public void setDocumentLocator(final Locator locator) {

this.locator = locator; // Save the locator, so that it can be used later for line tracking when traversing nodes.

}

@Override

public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)

throws SAXException {

addTextIfNeeded();

final Element el = doc.createElement(qName);

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

el.setAttribute(attributes.getQName(i), attributes.getValue(i));

}

el.setUserData(LINE_NUMBER_KEY_NAME, String.valueOf(this.locator.getLineNumber()), null);

elementStack.push(el);

}

@Override

public void endElement(final String uri, final String localName, final String qName) {

addTextIfNeeded();

final Element closedEl = elementStack.pop();

if (elementStack.isEmpty()) { // Is this the root element?

doc.appendChild(closedEl);

} else {

final Element parentEl = elementStack.peek();

parentEl.appendChild(closedEl);

}

}

@Override

public void characters(final char ch[], final int start, final int length) throws SAXException {

textBuffer.append(ch, start, length);

}

// Outputs text accumulated under the current node

private void addTextIfNeeded() {

if (textBuffer.length() > 0) {

final Element el = elementStack.peek();

final Node textNode = doc.createTextNode(textBuffer.toString());

el.appendChild(textNode);

textBuffer.delete(0, textBuffer.length());

}

}

};

parser.parse(is, handler);

return doc;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值