이
에서만 얻을 수있을 것 ... 나는이 스레드 (죄송합니다) 조금 오래 알고 있지만, 내가이 사람과 솔루션을 공유했다이 너트 균열 나를 너무 오래 촬영하고있다 DOM을 구축하지 않는 SAX의 행 번호. DOM 파서는 줄 번호를 제공하지 않으며 사용하지 않는 SAX 파서 가까이에 두지 않습니다. 내 솔루션은 SAX 소스와 DOM 결과를 사용하여 빈 XSLT 변환을 수행하는 것입니다.하지만이 경우에도 누군가 숨기기 위해 최선을 다했습니다. 아래 코드를 참조하십시오.
위치 정보를 고유 한 네임 스페이스가있는 속성으로 각 요소에 추가하므로 XPath를 사용하여 요소를 찾고 데이터의 출처를보고 할 수 있습니다.
희망이 도움이 :
이
// The file to parse.
String systemId = "myxml.xml";
/*
* Create transformer SAX source that adds current element position to
* the element as attributes.
*/
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
LocationFilter locationFilter = new LocationFilter(xmlReader);
InputSource inputSource = new InputSource(new FileReader(systemId));
// Do this so that XPath function document() can take relative URI.
inputSource.setSystemId(systemId);
SAXSource saxSource = new SAXSource(locationFilter, inputSource);
/*
* Perform an empty transformation from SAX source to DOM result.
*/
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMResult domResult = new DOMResult();
transformer.transform(saxSource, domResult);
Node root = domResult.getNode();
...
class LocationFilter extends XMLFilterImpl {
LocationFilter(XMLReader xmlReader) {
super(xmlReader);
}
private Locator locator = null;
@Override
public void setDocumentLocator(Locator locator) {
super.setDocumentLocator(locator);
this.locator = locator;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// Add extra attribute to elements to hold location
String location = locator.getSystemId() + ':' + locator.getLineNumber() + ':' + locator.getColumnNumber();
Attributes2Impl attrs = new Attributes2Impl(attributes);
attrs.addAttribute("http://myNamespace", "location", "myns:location", "CDATA", location);
super.startElement(uri, localName, qName, attrs);
}
}
좋아, 나는 그것을 확인해 보겠습니다. 이 질문은 실제로 1.4/5.0의 기본 SAX 프로세서에서는 가능하지 않다고 말하는 잘못된 정보를 소비 한 것 같습니다. 내가 성공하면 여기서 다시보고 할게. – Chris
고마워, 정확히 내가 무엇을 찾고 있었는지. – jutky