gpt4 book ai didi

java - 使用dom4j定位有行号的节点

转载 作者:行者123 更新时间:2023-11-30 08:03:58 26 4
gpt4 key购买 nike

我用dom4j解析了一个xml文件,得到了一个带行号的文档,我想用行号定位所有带行号的节点,并操作这些节点。

有没有办法使用 dom4j 或其他 DOM API 来实现它?

最佳答案

可以扩展 DOM4J 以包含元素的行号,但这有点令人费解,而且不是 100% 准确(您可以获取开始元素的“>”字符的行号)。

可能,一种更可靠的方法是报告每个有问题的元素的 XPath 表达式,然后使用这些表达式来修复它。

但是,为了好玩,下面是一个如何在 DOM4J 中包含行号信息的完整示例:

public class LineNumber {

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

SAXReader reader = new MySAXReader();
reader.setDocumentFactory(new LocatorAwareDocumentFactory());

Document doc = reader
.read(new StringReader("<root foo='bar' > \n<alfa/>\n<beta/>\n<gamma/>\n</root>\n"));
doc.accept(new VisitorSupport() {
@Override
public void visit(Element node) {
System.out.printf("%d: %s\n",
((LocationAwareElement) node).getLineNumber(),
node.getName());
}
});

}

static class MySAXReader extends SAXReader {

@Override
protected SAXContentHandler createContentHandler(XMLReader reader) {
return new MySAXContentHandler(getDocumentFactory(),
getDispatchHandler());
}

@Override
public void setDocumentFactory(DocumentFactory documentFactory) {
super.setDocumentFactory(documentFactory);
}

}

static class MySAXContentHandler extends SAXContentHandler {

private Locator locator;

// this is already in SAXContentHandler, but private
private DocumentFactory documentFactory;

public MySAXContentHandler(DocumentFactory documentFactory,
ElementHandler elementHandler) {
super(documentFactory, elementHandler);
this.documentFactory = documentFactory;
}

@Override
public void setDocumentLocator(Locator documentLocator) {
super.setDocumentLocator(documentLocator);
this.locator = documentLocator;
if (documentFactory instanceof LocatorAwareDocumentFactory) {
((LocatorAwareDocumentFactory)documentFactory).setLocator(documentLocator);
}

}

public Locator getLocator() {
return locator;
}
}

static class LocatorAwareDocumentFactory extends DocumentFactory {

private Locator locator;

public LocatorAwareDocumentFactory() {
super();
}

public void setLocator(Locator locator) {
this.locator = locator;
}

@Override
public Element createElement(QName qname) {
LocationAwareElement element = new LocationAwareElement(qname);
if (locator != null)
element.setLineNumber(locator.getLineNumber());
return element;
}

}

/**
* An Element that is aware of it location (line number in) in the source document
*/
static class LocationAwareElement extends DefaultElement {

private int lineNumber = -1;

public LocationAwareElement(QName qname) {
super(qname);
}

public LocationAwareElement(QName qname, int attributeCount) {
super(qname, attributeCount);

}

public LocationAwareElement(String name, Namespace namespace) {
super(name, namespace);

}

public LocationAwareElement(String name) {
super(name);

}

public int getLineNumber() {
return lineNumber;
}

public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}

}

}

关于java - 使用dom4j定位有行号的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com