Java 在netbeans平台编辑器cookie中获取选定的xml标记
我正在开发一个netbeans平台应用程序,我有一个自定义xml编辑器。 xml文档与此类似(这是一个示例): 在actionPerformed方法中执行以下操作:Java 在netbeans平台编辑器cookie中获取选定的xml标记,java,xml,cookies,netbeans,netbeans-platform,Java,Xml,Cookies,Netbeans,Netbeans Platform,我正在开发一个netbeans平台应用程序,我有一个自定义xml编辑器。 xml文档与此类似(这是一个示例): 在actionPerformed方法中执行以下操作: CloneableEditorSupport editorSupport = (CloneableEditorSupport )context; try { InputStream inputStream = editorSupport.getInputStream(); InputSourc
CloneableEditorSupport editorSupport = (CloneableEditorSupport )context;
try {
InputStream inputStream = editorSupport.getInputStream();
InputSource inputSource = new InputSource(inputStream);
Document xmlDoc = XMLUtil.parse(inputSource, true, true, null, new RegisterCatalog());
NodeList nodeList = xmlDoc.getElementsByTagName("sentence");
...
在第二个代码段中,我可以访问所选行:
JEditorPane[] panes = context.getOpenedPanes();
if (panes != null) {
if (panes.length > 0) {
JEditorPane pane = panes[0];
AccessibleContext ac = pane.getAccessibleContext();
AccessibleText accessibleText = ac.getAccessibleText();
int caretPosition = accessibleText.getCaretPosition();
StyledDocument document = context.getDocument();
Element characterElement = document.getCharacterElement(caretPosition);
int start = characterElement.getStartOffset();
int end = characterElement.getEndOffset();
}
}
在开始和结束位置,我得到:
<sentence id="2">
但我想要的是右键点击第二句话的那一行
我想得到:
<sentence id="2">
<word id="1">This</word>
<word id="2">is</word>
<word id="3">another</word>
<word id="4">sentence</word>
<word id="5">.</word>
</sentence>
这
是
另一个
句子
.
是否有可能访问整个标签,包括其结束标签?另一个问题是线路的可比性,因为
<sentence id="2">
我无法唯一地确定选择了哪一个,因为有两个id为“2”的句子,我需要区分选择了哪一个。
我希望你能帮助我:)
<sentence id="2">
<word id="1">This</word>
<word id="2">is</word>
<word id="3">another</word>
<word id="4">sentence</word>
<word id="5">.</word>
</sentence>
<sentence id="2">