Java 如何使用DOM选择具有不同标记的节点?
我有一个xml文件,看起来像:Java 如何使用DOM选择具有不同标记的节点?,java,xml,dom,domparser,Java,Xml,Dom,Domparser,我有一个xml文件,看起来像: <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> <HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <NE MOID="WBTS-42" NEType="WBTS"> <EQHO MOID="EQHO-1-0" > <UNIT MOID="UN
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NE MOID="WBTS-42" NEType="WBTS">
<EQHO MOID="EQHO-1-0" >
<UNIT MOID="UNIT-FAN-1" State="enabled"></UNIT>
<UNIT MOID="UNIT-FAN-3" State="enabled"></UNIT>
</EQHO>
</NE>
<NE MOID="RNC-40" NEType="RNC">
<EQHO MOID="EQHO-3-0" >
<UNIT MOID="UNIT-FAN-5" State="disabled"></UNIT>
<UNIT MOID="UNIT-FAN-6" State="disabled"></UNIT>
</EQHO>
</NE>
</HWData>
我在问如何使用DOM获得包含“NE”和“UNIT”标记的节点列表?
谢谢您可以手动操作:
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlDomTest {
public static void main(String[] args) throws Exception {
File file = new File("/path/to/your/file");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
Set<String> filteredNames = new HashSet<String>(Arrays.asList("NE", "UNIT"));
NodeList list = collectNodes(doc, filteredNames);
for (int i = 0; i < list.getLength(); i++)
System.out.println(list.item(i).getNodeName());
}
private static NodeList collectNodes(Document doc, Set<String> filteredNames) {
Node ret = doc.createElement("NodeList");
collectNodes(doc, filteredNames, ret);
return ret.getChildNodes();
}
private static void collectNodes(Node node, Set<String> filteredNames, Node ret) {
NodeList chn = node.getChildNodes();
for (int i = 0; i < chn.getLength(); i++) {
Node child = chn.item(i);
if (filteredNames.contains(child.getNodeName()))
ret.appendChild(child);
collectNodes(child, filteredNames, ret);
}
}
}
导入java.io.File;
导入java.util.array;
导入java.util.HashSet;
导入java.util.Set;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公共类XmlDomTest{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“/path/to/your/File”);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文档doc=db.parse(文件);
Set filteredNames=newhashset(Arrays.asList(“NE”,“UNIT”));
节点列表=收集节点(文档、过滤器名称);
对于(int i=0;i
试试这个:
public static List<String> MOIDList(File file) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression exp = xPath.compile("//NE | //UNIT");
NodeList nl = (NodeList)exp.evaluate(doc, XPathConstants.NODESET);
List<String> MoidList = new ArrayList<>();
for (int i = 0; i < nl.getLength(); i++) {
String moid=((Element)nl.item(i)).getAttribute("MOID");
MoidList.add(moid);
}
return MoidList;
}
publicstaticlist MOIDList(File文件)抛出SAXException、IOException、ParserConfigurationException、XPathExpressionException{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文档doc=db.parse(文件);
XPath=XPathFactory.newInstance().newXPath();
XPathExpression exp=xPath.compile(“/NE |//UNIT”);
NodeList nl=(NodeList)exp.evaluate(doc,XPathConstants.NODESET);
List MoidList=new ArrayList();
对于(int i=0;i
仅选择MOID
的xpath是//NE/@MOID |//UNIT/@MOID
您应该看看我的开源Xml解析器库。可以在网上买到
然后,您可以执行以下操作:
如果需要,还可以创建更复杂的嵌套数据结构。如果您想要一个关于如何操作的示例,请在这里给我一个注释。您是否要求一个带有6个值的
节点列表?我的意思是,具有以下MOID的6个元素:WBTS-42
,UNIT-FAN-1
,UNIT-FAN-3
,RNC-40
,UNIT-FAN-5
,UNIT-FAN-6
DOM中没有内置的方法。您可以获得两个节点列表,一个用于NE
元素,另一个用于UNIT
元素。不过,您可以使用XPath。请参阅本文了解更多详细信息。好的@Andreas我认为有一种方法可以使用DOM实现这一点,现在我将尝试使用XPath谢谢
import com.nerdforge.unxml.Parsing;
import com.nerdforge.unxml.factory.ParsingFactory;
import org.w3c.dom.Document;
import java.util.List;
public class Parser {
public List<String> parseXml(String xml){
Parsing parsing = ParsingFactory.getInstance().create();
Document document = parsing.xml().document(xml);
List<String> result = parsing
.arr("//NE/@MOID | //UNIT/@MOID", parsing.text())
.as(String.class)
.apply(document);
return result;
}
}
[WBTS-42, UNIT-FAN-1, UNIT-FAN-3, RNC-40, UNIT-FAN-5, UNIT-FAN-6]