gpt4 book ai didi

java - stax xml 与 getname 函数混淆

转载 作者:行者123 更新时间:2023-11-30 02:58:34 25 4
gpt4 key购买 nike

我有一个像这样的 xml 文件:

<comment type="PTM">
<text evidence="19">Sumoylated following its interaction with PIAS1 and UBE2I.</text>
</comment>
<comment type="PTM">
<text evidence="17">Ubiquitinated, leading to proteasomal degradation.</text>
</comment>
<comment type="disease">
<text>A chromosomal aberration involving ZMYND11 is a cause of acute poorly differentiated myeloid leukemia. Translocation (10;17)(p15;q21) with MBTD1.</text>
</comment>
<comment type="disease" evidence="23">
<disease id="DI-04257">
<name>Mental retardation, autosomal dominant 30</name>
<acronym>MRD30</acronym>
<description>A disorder characterized by significantly below average general intellectual functioning associated with impairments in adaptive behavior and manifested during the developmental period. MRD30 patients manifest mild intellectual disability and subtle facial dysmorphisms, including hypertelorism, ptosis, and a wide mouth.</description>
<dbReference type="MIM" id="616083"/>
</disease>
<text>The disease is caused by mutations affecting the gene represented in this entry.</text>
</comment>
<comment type="similarity">
<text evidence="8">Contains 1 bromo domain.</text>
</comment>
<comment type="similarity">
<text evidence="9">Contains 1 MYND-type zinc finger.</text>
</comment>

我使用 Stax 来提取疾病信息。这是我的代码的一部分:

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader( new FileReader(p));

while(eventReader.hasNext()){
XMLEvent event = eventReader.nextEvent();
switch(event.getEventType()){
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("comment")) {
System.out.println("Start Element : comment");
Iterator<Attribute> attributes = startElement.getAttributes();
Attribute a = attributes.next();
System.out.println("ATRIBUTES " + a.getName());
type = a.getValue();
System.out.println("Roll No : " + type);
} else if(qName.equalsIgnoreCase("text") && type.equals("disease")){ text = true; }

break;

case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if(text){ res = res + " " + characters.getData();
//System.out.println("TEXT: " + res);
text = false;
}
break;

case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if(endElement.getName().getLocalPart().equalsIgnoreCase("comment")){
//System.out.println("End Element : comment");
//System.out.println();
}
break;

对于这种类型的线路:

<comment type="disease">

我可以正确提取信息,但是当我尝试在这一行中查找评论时键入“disease”:

<comment type="disease" evidence="23">

它给了我类型=证据,而不是应有的类型=疾病。因此它不会保存此类行中的任何内容。

最佳答案

首先,我们能否养成使用有用变量名称的习惯,您有以下变量及其类型:a(node)、text(boolean ), qName(String)...这些变量让我摸不着头脑,想知道它们是什么:

a - 只是不是一个有用的名称,它实际上应该类似于 typeAttr 或注意到它应该是 type="" 属性

text - 它是一个 boolean 值?!也许 collectText 会更合适,因为它指定您应该收集下一个文本事件值。

qName - 它是一个字符串,是 QName 的 localPart,如果它不是 QName,则不要将其命名为一个。

<小时/>

但咆哮已经够多了,你明白了。您的问题在于您获取属性的位置。在 XML 中,属性没有特定的顺序,并且不会也不应该期望按照它们定义的顺序返回。在您的代码中有以下内容

Iterator<Attribute> attributes = startElement.getAttributes();
Attribute a = attributes.next();
System.out.println("ATRIBUTES " + a.getName());
type = a.getValue();

在这里,您从元素中获取第一个属性并将类型设置为其值。正如我提到的,XML 属性没有特定的顺序,因此您将获得 evidence 属性。您应该按名称获取属性:

Attribute a = startElement.getAttributeByName(QName.valueOf("type"));
System.out.println("ATRIBUTES " + a.getName());
type = a.getValue();

关于java - stax xml 与 getname 函数混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36532623/

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