简体   繁体   English

使用多个相同的标签解析XML

[英]Parsing XML with multiple, identical tags

Before adding anything else, I would like to mention that I have looked at other answers here. 在添加任何其他内容之前,我想提一下,我在这里已经查看了其他答案。 Unfortunately the answers didn't apply to my situation, and the best one just provided a smattering of code without actually answering anything. 不幸的是,答案并不适合我的情况,最好的答案只是提供了一些代码,而没有实际回答任何问题。

I have XML files with contents such as this: 我有包含如下内容的XML文件:

<TransactionLine status="normal">
 <ItemLine>
  <ItemCode>
   <POSCodeFormat format="upcA"></POSCodeFormat>
   <POSCode>074804007527</POSCode>
   <POSCodeModifier name="pc">1</POSCodeModifier>
  </ItemCode>
  <Description>EP  PK WINS EACH</Description>
  <EntryMethod method="scan"></EntryMethod>
  <ActualSalesPrice>2.99</ActualSalesPrice>
  <MerchandiseCode>1</MerchandiseCode>
  <SellingUnits>1</SellingUnits>
  <RegularSellPrice>2.99</RegularSellPrice>
  <SalesQuantity>1</SalesQuantity>
  <SalesAmount>2.99</SalesAmount>
  <ItemTax>
   <TaxLevelID>101</TaxLevelID>
  </ItemTax>
  <SalesRestriction>
   <SalesRestrictFlag value="no"  type="other"></SalesRestrictFlag>
  </SalesRestriction>
 </ItemLine>
</TransactionLine>
<TransactionLine status="normal">
<ItemLine>
<ItemCode>
 <POSCodeFormat format="upcA"></POSCodeFormat>
 <POSCode>030004344770</POSCode>
 <POSCodeModifier name="pc">1</POSCodeModifier>
</ItemCode>
<Description>MCRFBER TOW EACH</Description>
<EntryMethod method="scan"></EntryMethod>
<ActualSalesPrice>1</ActualSalesPrice>
<MerchandiseCode>1</MerchandiseCode>
<SellingUnits>1</SellingUnits>
<RegularSellPrice>1</RegularSellPrice>
<SalesQuantity>1</SalesQuantity>
<SalesAmount>1</SalesAmount>
<ItemTax>
 <TaxLevelID>101</TaxLevelID>
</ItemTax>
<SalesRestriction>
 <SalesRestrictFlag value="no"  type="other"></SalesRestrictFlag>
</SalesRestriction>
</ItemLine>
</TransactionLine>

A given file will have multiple "Transaction Lines". 给定的文件将具有多个“交易行”。 The differentiating factor between them would be the POS Code. 它们之间的区别因素将是POS代码。 My main problem is, how do I drill down to the point where I can actually use that differentiating value to start tossing information into the necessary objects? 我的主要问题是,如何深入到实际可以使用差异值开始将信息投入必要对象的地步? Simply removing them as I go isn't an option. 我不能随便删除它们。 I can't control the XML output, so I can't make it more usable. 我无法控制XML输出,因此无法使其更加有用。 I'm using XStrem as the XML parser. 我正在使用XStrem作为XML解析器。 Solutions that are in Java are preferable, but Scala is also okay. 最好使用Java解决方案,但是Scala也可以。

Taking for grant your xml contains a root node, you could use an xpath expression to identify the pertinent node, something like: 以为您的xml包含一个根节点,您可以使用xpath表达式来标识相关节点,例如:

//TransactionLine/ItemLine/ItemCode[POSCode=074804007527]/../..

using XStream should be something like 使用XStream应该像

String idimlookingfor = "074804007527";

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();

String xpathexpression = String.format("//TransactionLine/ItemLine/ItemCode[POSCode=%s]/../..", idimlookingfor);
XPathExpression expr = xpath.compile(xpathexpression);

Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM