当前位置: 首页 > 知识库问答 >
问题:

从xml获取特定值

宰父嘉胜
2023-03-14

我有一个XML文件,我想取两个值。以下是XML:

<?xml version="1.0" encoding="UTF-8"?>
<orders>
    <order created='2012-07-12T15:29:33.000' ID='2343'>
        <product>
            <description>Sony 54.6" (Diag) Xbr Hx929 Internet Tv</description>
            <gtin>00027242816657</gtin>
            <price currency="USD">2999.99</price>
            <supplier>Sony</supplier>
        </product>
        <product>
            <description>Apple iPad 2 with Wi-Fi 16GB - iOS 5 - Black</description>
            <gtin>00885909464517</gtin>
            <price currency="USD">399.0</price>
            <supplier>Apple</supplier>
        </product>
        <product>
            <description>Sony NWZ-E464 8GB E Series Walkman Video MP3 Player Blue</description>
            <gtin>00027242831438</gtin>
            <price currency="USD">91.99</price>
            <supplier>Sony</supplier>
        </product>
    </order>
    <order created='2012-07-13T16:02:22.000' ID='2344'>
        <product>
            <description>Apple MacBook Air A 11.6" Mac OS X v10.7 Lion MacBook</description>
            <gtin>00885909464043</gtin>
            <price currency="USD">1149.0</price>
            <supplier>Apple</supplier>
        </product>
        <product>
            <description>Panasonic TC-L47E50 47" Smart TV Viera E50 Series LED HDTV</description>
            <gtin>00885170076471</gtin>
            <price currency="USD">999.99</price>
            <supplier>Panasonic</supplier>
        </product>
    </order>
</orders>

我想让我们说一下<代码>

这是我尝试过的代码,但它不起作用:

public void readXml() throws ParserConfigurationException, SAXException, IOException {
    
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document doc = builder.parse(new File("C:\\Users\\40723\\Documents\\NetBeansProjects\\SACOM\\src\\sacom\\suppliers23.xml"));
    NodeList nodeList = doc.getElementsByTagName("orders");
    
    for(int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if(node.hasAttributes()) {
            Attr attr = (Attr) node.getAttributes().getNamedItem("supplier");
            if(attr != null) {
                String attribute = attr.getValue();
                System.out.println("attribute: " + attribute);
            }
        }
    }
    
}

有人能给我一个工作的例子吗?我是Java和XML的新手。

共有1个答案

从建明
2023-03-14

我建议阅读XML规范,以了解其术语。

在XML中,元素是以标签开头的数据,例如

在XML中,属性是元素开始标记内的name=值对。

<代码>

<代码>

<代码>

所有这些意味着您不应该查看属性。完全

定位特定元素的最简单方法是使用XPath:

Document doc = builder.parse(new File("C:\\Users\\40723\\Documents\\NetBeansProjects\\SACOM\\src\\sacom\\suppliers23.xml"));
NodeList productElements = (NodeList)
    XPathFactory.newInstance().newXPath().evaluate(
        "//product[supplier='Apple']", doc, XPathConstants.NODESET);

如果要坚持使用DOM(文档对象模型),可以这样做,但需要查找元素:

Document doc = builder.parse(new File("C:\\Users\\40723\\Documents\\NetBeansProjects\\SACOM\\src\\sacom\\suppliers23.xml"));

NodeList nodeList = doc.getElementsByTagName("product");
int productCount = nodeList.getLength();

List<Element> matchingProductElements = new ArrayList<>(productCount);

for (int i = 0; i < productCount; i++) {
    Element product = (Element) nodeList.item(i);

    NodeList supplierElements = product.getElementsByTagName("supplier");
    if (supplierElements.getLength() > 0) {
        String supplierValue = supplierElements.item(0).getTextContent();
        if ("Apple".equals(supplierValue)) {
            matchingProductElements.add(product);
        }
    }
}

1。在注释、序言、处理指令或CDATA部分之外

 类似资料:
  • } 我想从这个json对象中获取值。 我在下面试过了,但没有成功。

  • 这是我的XML源: https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml 如何获取DKK(丹麦克朗)的最新汇率? 我不想使用foreach并检查货币是否为DKK... 我需要这样的东西: 知道怎么解决这个问题吗?

  • 我最近开始使用rest assured测试一些XML。下面是一个例子: 我想在这里实现的是,检查每次购物是否有一个容器号="8",然后检查每个容器是否有颜色="绿色"。这是代码: 这工作得非常好,当我调试这一点时,我得到了我想要实现的东西: 这是一个三元素列表,其中每个元素都等于“绿色”。 但当我试图检查每一次购物是否都有一个类别type=“present”,然后检查这些类别是否都有label=“

  • 我正在使用forkify API制作一个配方搜索应用程序。我得到了这样的json(以披萨食谱为例)。我已经做了一个回收和搜索,但食谱本身是作为一个链接提供给该网站的配方(见json中的source_url)。我已经为此制作了一个网络视图,但有一个问题。我需要得到那个源url,并让它与我点击的菜谱匹配。我试图在Rescycler中创建一个额外的元素,小的不可见文本视图,并将source_url放在那

  • 问题内容: 这是一项学术任务,我们将获得一个非常大的XML文件,其中包含数百个此类条目。对于每个项目,我们都应列出经理的ID,将项目添加到列表中的最后一个人员的人员ID以及当前的项目数。我已阅读并重新阅读了Oracle DOM API和各种Node API。我们正在使用JAVA,我一生都无法弄清楚如何搜索每个节点的各个“字段” 。以下是我们提供的数据示例。 我尝试做类似的事情: 并弄乱了这段代码一

  • 当我打印我的API响应时,它给出了下面的xml作为响应: