gpt4 book ai didi

java - 如何在java中获取具有两个属性的xml元素的文本

转载 作者:行者123 更新时间:2023-11-30 04:30:09 24 4
gpt4 key购买 nike

我想获取具有两个属性的元素内的文本,示例 xml 如下

    <?xml version="1.0" encoding="UTF-8"?>
<queries>
<query pagename="master" param="default">
SELECT * from test;
</query>
<query pagename="uftl" param="default">
SELECT uftl, lop from dwells where lop='a'
</query>
</queries>

输入:两个属性,输出:查询。即,在将输入指定为“master”、“default”时,我想获取该元素的查询,在本例中为“SELECT * from test;”

最佳答案

哦。我在等待你的答案时编写 dom 解析器

private String parse(Document document) {
Element root = document.getDocumentElement();
NodeList queries = root.getElementsByTagName("queries");
int queriesLength = queries.getLength();
for (int i = 0; i < queriesLength; i++) {
Element currentQuery = (Element) queries.item(i);
if (currentQuery.getNodeType() == Element.ELEMENT_NODE) {
String pagename = currentQuery.getAttributes()
.getNamedItem("pagename").getTextContent();
String param = currentCategory.getAttributes()
.getNamedItem("param").getTextContent();
if(param.equals(paramValue) && pagename.equals(pagename)){
String query = currnetNode.item(0).getTextContent();
return query;
}
return null;
}
}
}

SAX 解析器:

public class parser implements ContentHandler {
boolean check = false;
ArrayList<String> queries = new ArrayList<>();

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
switch (localName) {
case "query":
String param = atts.getValue("param");
String pagename = atts.getValue("pagename");
check = true;
break;
default:
return;
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
check = false;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String tagContent = new String(ch, start, length).trim();
if(check){
if(!tagContent.isEmpty()){
queries.add(tagContent);
}
}
}

我删除了 sum 重写方法,因为它们在这里是空的并且是不必要的。您必须实现它们并留空

更新:

主类:

public class Main {
public static void main(String[] args) throws IOException, SAXException {
ArrayList<String> queries = new parser().getQueries("test.xml");
for (String query : queries){
System.out.println(query);
}

}
}

解析器类:

public class parser implements ContentHandler {
boolean check = false;
ArrayList<String> queries = new ArrayList<>();

public ArrayList<String> getQueries(String fileName) throws SAXException, IOException {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(this);
xmlReader.parse(fileName);
return queries;
}

@Override
public void setDocumentLocator(Locator locator) {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void startDocument() throws SAXException {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void endDocument() throws SAXException {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void endPrefixMapping(String prefix) throws SAXException {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
switch (localName) {
case "query":
String param = atts.getValue("param");
String pagename = atts.getValue("pagename");
if(!param.isEmpty() && !pagename.isEmpty())
check = true;
break;
default:
return;
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
check = false;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String tagContent = new String(ch, start, length).trim();
if(check){
if(!tagContent.isEmpty()){
queries.add(tagContent);
}
}
}

@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void processingInstruction(String target, String data) throws SAXException {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void skippedEntity(String name) throws SAXException {
//To change body of implemented methods use File | Settings | File Templates.
}
}

我还在项目的根目录中添加了名为 test.xml

的 xml 文件

我的输出如下所示:

SELECT * from test;
SELECT uftl, lop from dwells where lop='a'

关于java - 如何在java中获取具有两个属性的xml元素的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14825994/

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